当前位置:网站首页>SWUST oj668: the thief ran away

SWUST oj668: the thief ran away

2022-06-11 10:48:00 Studying hard

Title Description

Recently, the thieves in the East Sixth dormitory building are very smart , They can always find a way to escape after stealing , To catch them , We need to know their escape route , Please help find out their escape route ( To simplify the problem , We guarantee that there is at most one escape route , And the starting point is ( 0 , 0 ), End ( 4 , 4) by , You can't run away with a slash , If someone intercepts at the end , Also for the escape failure ).

Input

 One 5*5 matrix , Space off 
0 Represents a walkable path ,1 Indicates an obstacle , The starting point of escape is the upper left , The end point is lower right 

Output

 Escape route , See output example ( Please note that x,y Axis direction ) If there is no way to escape , The output : "No Way!"( No colons or quotes )

The sample input

0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
0 1 1 1 1
0 0 0 0 0

Sample output

(0,0)
(0,1)
(0,2)
(0,3)
(0,4)
(1,4)
(2,4)
(3,4)
(4,4)
#include<stdio.h>
#include<string.h>
#define INF 0x3f3f3f3f
#define MAX(x,y) (x>y?x:y)
#define MIN(x,y) (x>y?y:x)
#define M 10
int a[M][M];
int vis[M][M];
int dir[][2] = { {0,1},{1,0},{0,-1},{-1,0} };
int c[M][2];
int flag = 1;
void dfs(int x, int y, int step)
{
	if (x == 4 && y == 4)
	{
		for (int i = 0; i < step; i++)
		{
			printf("(%d,%d)\n", c[i][1], c[i][0]);
		}
		flag = 0;
		return;
	}
	for (int i = 0; i < 4; i++)
	{
		int xa = x + dir[i][0];
		int ya = y + dir[i][1];
		if (xa >= 0 && xa <= 4 && ya >= 0 && ya <= 4 && vis[xa][ya] == 0 && a[xa][ya] != 1)
		{
			vis[xa][ya] = 1;
			c[step][0] = xa;
			c[step][1] = ya;
			dfs(xa, ya, step + 1);
			c[step][0] = 0;
			c[step][1] = 0;
			vis[xa][ya] = 0;
		}
	}
}
int main()
{
	int i, j;
	for (i = 0; i < 5; i++)
	{
		for (j = 0; j < 5; j++)
		{
			scanf("%d", &a[i][j]);
		}
	}
	vis[0][0] = 1;
	c[0][0] = 0;
	c[0][1] = 0;
	dfs(0, 0, 1);
	if (flag == 1)
	{
		printf("No Way!\n");
	}
	return 0;
}

原网站

版权声明
本文为[Studying hard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203012231084914.html