当前位置:网站首页>[Jizhong] July 16, 2022 1432. Oil pipeline
[Jizhong] July 16, 2022 1432. Oil pipeline
2022-07-26 01:02:00 【SSL_ YZJ】
Quick links
Original title website
Title Description
Please help design a city M M M To the city Z Z Z Oil pipeline , Now the whole area has been divided into R R R That's ok C C C Column , Each cell may be empty or the following 7 7 7 One of the basic pipelines :
Oil from the city M M M flow Z Z Z,“ + + +” Type a pipe is special , Because oil must be in two directions ( Vertical and horizontal ) Up transfer , As shown in the figure below :
Now the terrorists have got the design of the oil pipeline , And stole the pipe in one of the cells , Please help find the location and shape of the stolen pipe .
Format
Input format
The first 1 1 1 Line inclusion 2 2 2 It's an integer R R R and C C C.
Next R R R Every line C C C Characters describe the shape after being stolen , The characters are divided into the following 3 3 3 Kind of :
- “ . . .” Said empty ;
- character “ ∣ | ∣”( A S C I I ASCII ASCII by 124 124 124)、“ − - −”、“ + + +”、“ 1 1 1”、“ 2 2 2”、“ 3 3 3”、“ 4 4 4” Describe the shape of the pipe ;
- “ M M M” and “ Z Z Z” Representing City , 2 2 2 All of them only appear 1 1 1 Time .
Input ensures that the flow direction of oil is unique , There is only one pipe with M M M and Z Z Z Connected to a , Besides , Make sure there are no extra pipes , That is to say, all the pipes will be used after being added to the stolen pipes .
The input is guaranteed to be solvable and unique .
Output format
Output the line number and column number of the stolen pipeline and the type of pipeline .
Examples
sample input
sample input 1
3 7
.......
.M-.-Z.
.......
sample input 2
3 5
..1-M
1-+..
Z.23.
sample input 3
6 10
Z.1----4..
|.|....|..
|..14..M..
2-+++4....
..2323....
..........
sample output
sample output 1
2 4 -
sample output 2
2 4 4
sample output 3
3 3 |
Tips
1 ≤ r , c ≤ 25 1\le r,c\le25 1≤r,c≤25
Their thinking
There are many ways to solve this problem , Such as b f s bfs bfs、 Big simulation, etc .
Because the big simulation is simple and rough , Loved by myself , And I am too l a z y lazy lazy, So there is no other algorithm .
Code
Warning
Code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,s;
char a[30][30];
bool check(char b[30][30],int x,int y,int f,int t) {
while(b[x][y]!='Z') {
t++;
switch(b[x][y]) {
case '-':{
if(f==3) y--;
else y++;
break;
}
case '|':{
if(f==1) x--;
else x++;
break;
}
case '+':{
if(f==1) x--;
else if(f==2) x++;
else if(f==3) y--;
else y++;
break;
}
case '1':{
if(f==1) {
y++;
f=4;
}
else {
x++;
f=2;
}
break;
}
case '2':{
if(f==2) {
y++;
f=4;
}
else {
x--;
f=1;
}
break;
}
case '3':{
if(f==2) {
y--;
f=3;
}
else {
x--;
f=1;
}
break;
}
case '4':{
if(f==1) {
y--;
f=3;
}
else {
x++;
f=2;
}
break;
}
default:{
return false;
}
}
}
return t-1==s;
}
bool test_left_down(int x,int y,int t) {
if(a[x+1][y]=='.'&&(a[x+1][y-1]=='-'||a[x+1][y-1]=='+'||
a[x+1][y-1]=='1'||a[x+1][y-1]=='2'||
a[x+1][y-1]=='Z')) {
a[x+1][y]='3';
if(check(a,x+1,y,2,t)) {
printf("%d %d 3",x+1,y);
return true;
}
a[x+1][y]='+';
if(check(a,x+1,y,2,t-1)) {
printf("%d %d +",x+1,y);
return true;
}
a[x+1][y]='.';
}
if(a[x][y-1]=='.'&&(a[x+1][y-1]=='|'||a[x+1][y-1]=='+'||
a[x+1][y-1]=='2'||a[x+1][y-1]=='3'||
a[x+1][y-1]=='Z')) {
a[x][y-1]='1';
if(check(a,x,y-1,3,t)) {
printf("%d %d 1",x,y-1);
return true;
}
a[x][y-1]='+';
if(check(a,x,y-1,3,t-1)) {
printf("%d %d +",x,y-1);
return true;
}
a[x][y-1]='.';
}
return false;
}
bool test_left_up(int x,int y,int t) {
if(a[x-1][y]=='.'&&(a[x-1][y-1]=='-'||a[x-1][y-1]=='+'||
a[x-1][y-1]=='1'||a[x-1][y-1]=='2'||
a[x-1][y-1]=='Z')) {
a[x-1][y]='4';
if(check(a,x-1,y,1,t)) {
printf("%d %d 4",x-1,y);
return true;
}
a[x-1][y]='+';
if(check(a,x-1,y,1,t-1)) {
printf("%d %d +",x-1,y);
return true;
}
a[x-1][y]='.';
}
if(a[x][y-1]=='.'&&(a[x-1][y-1]=='|'||a[x-1][y-1]=='+'||
a[x-1][y-1]=='1'||a[x-1][y-1]=='4'||
a[x-1][y-1]=='Z')) {
a[x][y-1]='2';
if(check(a,x,y-1,3,t)) {
printf("%d %d 2",x,y-1);
return true;
}
a[x][y-1]='+';
if(check(a,x,y-1,3,t-1)) {
printf("%d %d +",x,y-1);
return true;
}
a[x][y-1]='.';
}
return false;
}
bool test_right_down(int x,int y,int t) {
if(a[x+1][y]=='.'&&(a[x+1][y+1]=='-'||a[x+1][y+1]=='+'||
a[x+1][y+1]=='3'||a[x+1][y+1]=='4'||
a[x+1][y+1]=='Z')) {
a[x+1][y]='2';
if(check(a,x+1,y,2,t)) {
printf("%d %d 2",x+1,y);
return true;
}
a[x+1][y]='+';
if(check(a,x+1,y,2,t-1)) {
printf("%d %d +",x+1,y);
return true;
}
a[x+1][y]='.';
}
if(a[x][y+1]=='.'&&(a[x+1][y+1]=='|'||a[x+1][y+1]=='+'||
a[x+1][y+1]=='2'||a[x+1][y+1]=='3'||
a[x+1][y+1]=='Z')) {
a[x][y+1]='4';
if(check(a,x,y+1,4,t)) {
printf("%d %d 4",x,y+1);
return true;
}
a[x][y+1]='+';
if(check(a,x,y+1,4,t-1)) {
printf("%d %d +",x,y+1);
return true;
}
a[x][y+1]='.';
}
return false;
}
bool test_right_up(int x,int y,int t) {
if(a[x-1][y]=='.'&&(a[x-1][y+1]=='-'||a[x-1][y+1]=='+'||
a[x-1][y+1]=='3'||a[x-1][y+1]=='4'||
a[x-1][y+1]=='Z')) {
a[x-1][y]='1';
if(check(a,x-1,y,1,t)) {
printf("%d %d 1",x-1,y);
return true;
}
a[x-1][y]='+';
if(check(a,x-1,y,1,t-1)) {
printf("%d %d +",x-1,y);
return true;
}
a[x-1][y]='.';
}
if(a[x][y+1]=='.'&&(a[x-1][y+1]=='|'||a[x-1][y+1]=='+'||
a[x-1][y+1]=='1'||a[x-1][y+1]=='4'||
a[x-1][y+1]=='Z')) {
a[x][y+1]='3';
if(check(a,x,y+1,4,t)) {
printf("%d %d 3",x,y+1);
return true;
}
a[x][y+1]='+';
if(check(a,x,y+1,4,t-1)) {
printf("%d %d +",x,y+1);
return true;
}
a[x][y+1]='.';
}
return false;
}
bool test_down(int x,int y,int t) {
if(test_left_down(x,y,t)) return true;
if(test_right_down(x,y,t)) return true;
if(a[x+1][y]=='.'&&(a[x+2][y]=='|'||a[x+2][y]=='+'||
a[x+2][y]=='2'||a[x+2][y]=='3'||
a[x+2][y]=='Z')) {
a[x+1][y]='|';
if(check(a,x+1,y,2,t)) {
printf("%d %d |",x+1,y);
return true;
}
a[x+1][y]='+';
if(check(a,x+1,y,2,t-1)) {
printf("%d %d +",x+1,y);
return true;
}
a[x+1][y]='.';
}
return false;
}
bool test_up(int x,int y,int t) {
if(test_left_up(x,y,t)) return true;
if(test_right_up(x,y,t)) return true;
if(a[x-1][y]=='.'&&(a[x-2][y]=='|'||a[x-2][y]=='+'||
a[x-2][y]=='1'||a[x-2][y]=='4'||
a[x-2][y]=='Z')) {
a[x-1][y]='|';
if(check(a,x-1,y,1,t)) {
printf("%d %d |",x-1,y);
return true;
}
a[x-1][y]='+';
if(check(a,x-1,y,1,t-1)) {
printf("%d %d +",x-1,y);
return true;
}
a[x-1][y]='.';
}
return false;
}
bool test_right(int x,int y,int t) {
if(test_right_up(x,y,t)) return true;
if(test_right_down(x,y,t)) return true;
if(a[x][y+1]=='.'&&(a[x][y+2]=='-'||a[x][y+2]=='+'||
a[x][y+2]=='3'||a[x][y+2]=='4'||
a[x][y+2]=='Z')) {
a[x][y+1]='-';
if(check(a,x,y+1,4,t)) {
printf("%d %d -",x,y+1);
return true;
}
a[x][y+1]='+';
if(check(a,x,y+1,4,t-1)) {
printf("%d %d +",x,y+1);
return true;
}
a[x][y+1]='.';
}
return false;
}
bool test_left(int x,int y,int t) {
if(test_left_down(x,y,t)) return true;
if(test_left_up(x,y,t)) return true;
if(a[x][y-1]=='.'&&(a[x][y-2]=='-'||a[x][y-2]=='+'||
a[x][y-2]=='1'||a[x][y-2]=='2')) {
a[x][y-1]='-';
if(check(a,x,y-1,3,t)) {
printf("%d %d -",x,y-1);
return true;
}
a[x][y-1]='+';
if(check(a,x,y-1,3,t-1)) {
printf("%d %d +",x,y-1);
return true;
}
a[x][y-1]='.';
}
return false;
}
void work(int x,int y,int f,int t) {
t++;
switch(a[x][y]) {
case 'M':{
t--;
if(a[x-1][y]=='|'||a[x-1][y]=='1'||
a[x-1][y]=='4'||a[x-1][y]=='+') {
f=1;
work(x-1,y,f,t);
}
else if(a[x+1][y]=='|'||a[x+1][y]=='2'||
a[x+1][y]=='3'||a[x-1][y]=='+') {
f=2;
work(x+1,y,f,t);
}
else if(a[x][y-1]=='-'||a[x][y-1]=='+'||
a[x][y-1]=='1'||a[x][y-1]=='2') {
f=3;
work(x,y-1,f,t);
}
else if(a[x][y+1]=='-'||a[x][y+1]=='+'||
a[x][y+1]=='3'||a[x][y+1]=='4') {
f=4;
work(x,y+1,f,t);
}
else {
if(test_left(x,y,t)) return;
if(test_right(x,y,t)) return;
if(test_up(x,y,t)) return;
if(test_down(x,y,t)) return;
}
break;
}
case '|':{
if(f==1) {
if(a[x-1][y]!='.') work(x-1,y,f,t);
else {
test_up(x,y,t);
return;
}
}
else {
if(a[x+1][y]!='.') work(x+1,y,f,t);
else {
test_down(x,y,t);
return;
}
}
break;
}
case '-':{
if(f==3) {
if(a[x][y-1]!='.') work(x,y-1,f,t);
else {
test_left(x,y,t);
return;
}
}
else {
if(a[x][y+1]!='.') work(x,y+1,f,t);
else {
test_right(x,y,t);
return;
}
}
break;
}
case '+':{
if(f==1) {
if(a[x-1][y]!='.') work(x-1,y,f,t);
else {
test_up(x,y,t);
return;
}
}
else if(f==2) {
if(a[x+1][y]!='.') work(x+1,y,f,t);
else {
test_down(x,y,t);
return;
}
}
else if(f==3) {
if(a[x][y-1]!='.') work(x,y-1,f,t);
else {
test_left(x,y,t);
return;
}
}
else {
if(a[x][y+1]!='.') work(x,y+1,f,t);
else {
test_right(x,y,t);
return;
}
}
break;
}
case '1':{
if(f==1) {
if(a[x][y+1]!='.') work(x,y+1,4,t);
else {
test_right(x,y,t);
return;
}
}
else {
if(a[x+1][y]!='.') work(x+1,y,2,t);
else {
test_down(x,y,t);
return;
}
}
break;
}
case '2':{
if(f==2) {
if(a[x][y+1]!='.') work(x,y+1,4,t);
else {
test_right(x,y,t);
return;
}
}
else {
if(a[x-1][y]!='.') work(x-1,y,1,t);
else {
test_up(x,y,t);
return;
}
}
break;
}
case '3':{
if(f==2) {
if(a[x][y-1]!='.') work(x,y-1,3,t);
else {
test_left(x,y,t);
return;
}
}
else {
if(a[x-1][y]!='.') work(x-1,y,1,t);
else {
test_up(x,y,t);
return;
}
}
break;
}
case '4':{
if(f==1) {
if(a[x][y-1]!='.') work(x,y-1,3,t);
else {
test_left(x,y,t);
return;
}
}
else {
if(a[x+1][y]!='.') work(x+1,y,2,t);
else {
test_down(x,y,t);
return;
}
}
break;
}
}
}
signed main() {
cin>>n>>m;
int x,y;
for(int i=1;i<=n;i++) {
for(int j=1;j<=m;j++) {
cin>>a[i][j];
if(a[i][j]=='M') {
x=i,y=j;
}
else if(a[i][j]!='.'&&a[i][j]!='Z') s++;
if(a[i][j]=='+') s++;
}
}
work(x,y,0,0);
return 0;
}
Copyright
Icons provided by Bootstrap
边栏推荐
- What is the difference between request forwarding and request redirection?
- Nanjie's embarrassment
- Transfer learning - getting started
- matlab 按位 与 或 非
- The task will be launched before the joint commissioning of development
- 超全的开源Winform UI库,满足你的一切桌面开发需求!
- [translation paper] analysis of land cover classification using multi wavelength lidar system (2017)
- Distributed transaction and at mode principle of Seata
- 【秒杀概念】原反补
- Cnosdb Nirvana Rebirth: abandon go and fully embrace rust
猜你喜欢
![[RTOS training camp] course learning methods and C language knowledge (pointer, structure, function pointer, linked list) and student questions](/img/c4/b7ddf5527c27892676b50d2b873220.jpg)
[RTOS training camp] course learning methods and C language knowledge (pointer, structure, function pointer, linked list) and student questions

How can a team making facial mask achieve a revenue of more than 1 million a day?

Win11打不开自带杀毒软件怎么办?win11自带杀毒功能打不开

【RTOS训练营】课程学习方法和C语言知识(指针、结构体、函数指针、链表)和学员问题

Tensorflow 2 detailed explanation (TF ecosystem, installation, housekeeping, basic operation)

How to choose social e-commerce model in the early stage? Taishan crowdfunding

Processes and threads

【RTOS训练营】任务调度(续)、任务礼让、调度总结、队列和晚课提问
![[RTOS training camp] problems of evening students](/img/4a/9d781a28751c15e9e42cd5743e97db.jpg)
[RTOS training camp] problems of evening students

【RTOS训练营】站在更高的角度学习C语言
随机推荐
Selenium assertion and JS actuator
pip install --upgrade can‘t find Rust compiler
【秒杀概念】原反补
Unity get the animation being played
Solve the problem that when the background image is set to be 100% full, when the horizontal scroll bar appears in the zoom browser, the part of the background image beyond the scroll bar is not full
微波炉整流二极管 CL01-12
ZABBIX monitoring host and resource alarm
How to choose social e-commerce model in the early stage? Taishan crowdfunding
The task will be launched before the joint commissioning of development
[RTOS training camp] about classes and Q & A
【RTOS训练营】关于上课和答疑
Embedded development: tips and tricks -- seven tips for designing powerful boot loader
【软件开发规范四】《应用系统安全编码规范》
嵌入式开发:技巧和窍门——设计强大的引导加载程序的7个技巧
jupyter更改默认浏览器的方法
It will be easier to implement MES system by doing well in these four stages
Timeout settings for feign and hystrix
How can I become an irreplaceable programmer?
Implementation process of adding loading effect to easycvr page
【软件开发规范二】《禁止项开发规范》