DFS-Plumber Game


Today, I will review how to use DFS to find the channel that can connect the end to end and display it.

Title description

A piece of rectangular land is divided into N*M unit squares. Now some water pipes have been buried on this land. The water pipes will extend from the coordinate edge of the upper left corner of the rectangular land with coordinates (1,1) to coordinates (N, M) The right edge of the bottom right corner of the rectangular land. There are two types of water pipes, curved and straight.
Each type of pipeline will occupy a unit square land. You can now rotate these pipes to form a pipe system, that is, create a connecting pipe from (1,1) to (N,M). A square with a number indicates that there is no pipe.

enter

The first line of the input is two integers N and M (no more than 10), and the next N lines have M integers in each line, representing each cell in the map. Where 0 means the number, 1~6 respectively means six different ways of placing the pipe

Output

If a connected pipeline system can be formed by rotating the pipeline, the laid path is output, otherwise the output is impossible.

Input example 1

5 4
5 3 5 3
1 5 3 0
2 3 5 1
6 1 1 5
1 5 5 4

Sample output 1

(1,1) (1,2) (2,2) (3,2) (3,3) (3,4) (4,4) (5,4)

Input example 2

5 4
3 5 2 4
1 3 2 5
2 5 3 1
2 6 1 4
2 5 4 5

Sample output 2

impossible

AC code

#include<bits/stdc++.h>
using namespace std;
int book[51][51],a[51][51];
int next[5][2]={
    
    {
    
    0,0},{
    
    0,1},{
    
    1,0},{
    
    0,-1},{
    
    -1,0}};
int top;
int n,m,flag;  //flag 用来标记
struct note{
    
    
	int x,y;
}s[2501];  //用栈来存储路径

void dfs(int x,int y,int t)
{
    
    
	int tx,ty;
	if(x==n&&y==m+1){
    
      //如果有路径,标记flag,并输出路径
		flag=1;
		for(int i=1;i<=top;i++)
		printf("(%d,%d)",s[i].x,s[i].y);
		return;
	}
	
	if(x<1||x>n||y<1||y>m) return;  //防止越界
	
	if(book[x][y]==1) return ;   //如果已经走过
	
	book[x][y]=1;   //走过的路径标记下
	top++;   //入栈操作
	s[top].x=x;
	s[top].y=y;
	
	if(a[x][y]>=5&&a[x][y]<=6){
    
      //直管  有横竖 两种摆放方式
		if(t==1){
    
       //t=1说明管口在左侧
			dfs(x,y+1,1);   //管口在左侧,另一边就在右侧,就通过了右边的方格,所以y+1 ,  此时对右侧的方格而言,管口还是在左侧,所以是1
		}
		if(t==2){
    
       //t=2说明管口在上侧
			dfs(x+1,y,2);  //同理,
		}
		if(t==3){
    
       //t=3说明管口在右侧
			dfs(x,y-1,3);
		}
		if(t==4){
    
       //t=4说明管口在下侧
			dfs(x-1,y,4);
		}
	}
	
	if(a[x][y]>=1&&a[x][y]<=4){
    
    
		if(t==1){
    
    
			dfs(x-1,y,4);
			dfs(x+1,y,2);
		}
		if(t==2){
    
    
			dfs(x,y-1,3);
			dfs(x,y+1,1);
		}
		if(t==3){
    
    
			dfs(x-1,y,4);
			dfs(x+1,y,2);
		}
		if(t==4){
    
    
			dfs(x,y-1,3);
			dfs(x,y+1,1);
		}
	}
	
	book[x][y]=0;  //回溯
	top--;    
}
int main()
{
    
    
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++){
    
    
		for(int j=1;j<=m;j++){
    
    
			scanf("%d",&a[i][j]);
		}
	}
	
	dfs(1,1,1);
	
	if(!flag) printf("impossible");  //如果flag没有被标记,说明没有路径,输出impossible
	
	return 0;
} 

Alright, the problem is solved! ! ! !

Guess you like

Origin blog.csdn.net/qq_45735298/article/details/108146401