This has a maze, with 0~8 rows and 0~8 columns:
1,1,1,1,1,1,1,1,1
1,0,0,1,0,0,1,0,1
1,0,0,1,1,0,0,0,1
1,0,1,0,1,1,0,1,1
1,0,0,0,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,1,0,0,1
1,1,0,1,0,0,0,0,1
1,1,1,1,1,1,1,1,1
0 represents the road, and 1 represents the wall.
Now enter the coordinates of a road as the starting point, and then enter a road as the coordinates as the end point, ask at least a few steps to reach the end from the starting point?
(Note: One step is to move from one sitting punctuation to the next to the left and right adjacent coordinate points, such as: from (3,1) to (4,1). )
-
-
Input
-
-
the first line enters an integer n (0<n<=100), which indicates that there are n sets of test data;
Then n rows, each row has four integers a,b,c,d (0<=a,b,c,d<=8) representing the row, column, and end line of the starting point, respectively.
1211
method one:
#include <stdio.h> #include <string.h>int a,b,c,d;int min;int book[9][9];int s[9][9]={{1,1,1,1,1,1,1,1,1} , {1,0,0,1,0,0,1,0,1}, {1,0,0,1,1,0,0,0,1}, {1,0,1,0,1,1,0,1,1}, {1,0,0,0,0,1,0,0,1}, {1,1,0,1,0,1,0,0,1}, { 1,1,0,1,0,1,0,0,1}, {1,1,0,1,0,0,0,0,1}, {1,1,1,1,1,1,1,1,1}};void dfs (int x,int y,int step) {int next[4][2]={{ 0,1},{1,0},{0,-1},{-1,0}};if (X==c&&y==d) {if (step<min) Min=step;return;} int K,tx,ty;for (k=0;k<=3;k++) {tx=x+next[k][0];ty=y+next[k][1];if (tx>8| |ty>8| |tx<0| | ty<0) continue; if (s[tx][ty]==0&&book[tx][ty]==0) { book[tx][ty]=1; DFS (tx,ty,step+1); book[tx][ty]=0; } } return;} int main (void) {int n,i,j;scanf ("%d", &n), while (n--) {memset (book,0,sizeof (book)); MIN=99999999;SCANF ("%d%d%d%d" , &a,&b,&c,&d); Book[a][b]=1;dfs (a,b,0);p rintf ("%d\n", Min);} return 0;}
Method Two:
#include <stdio.h>int arr[9][9]={1,1,1,1,1,1,1,1,1, 1,0,0,1,0,0,1,0,1, 1,0,0,1,1,0,0,0,1, 1,0,1,0,1,1,0,1,1, 1,0,0,0,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,1,0,0,1, 1,1,0,1,0,0,0,0,1, 1,1,1,1,1,1,1,1,1};; int a,b,c,d,m;void dfs (int i,int j,int s) {if (arr[i][j]==1) return; if (i==c&&j==d) { m=s>m?m:s; return; } s=s+1; Arr[i][j]=1; DFS (I+1,J,S); DFS (I,J+1,S); DFS (I-1,J,S); DFS (I,J-1,S); arr[i][j]=0;} int main () {int t; scanf ("%d", &t), while (t--) { m=20000000; scanf ("%d%d%d%d", &a,&b,&c,&d); DFS (a,b,0); printf ("%d\n", m); } return 0;}