#include<string.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int vis[107][107],mapp[107][107],mark[40];
int sum,xx,yy,next;
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
int judge ( int x, int y)
{
if ( x< 0 || y < 0 || x > 4 || y > 4 )
{
return 0;
}
if ( mapp[x][y] == 1 )
{
return 0;
}
return 1;
}
void print ( int pos )
{
if ( mark[pos] != -1 )
{
print ( mark[pos] );
}
printf("(%d, %d)\n",pos/5,pos%5);
}
void bfs()
{
memset(vis,0,sizeof(vis));
queue<int> q;
mark[0]=-1;
vis[0][0]=1;
int s=0;
q.push(s);
while (!q.empty())
{
s=q.front(); q.pop();
int x=s/5;
int y=s%5;
for (int i=0; i<4; i++)
{
xx=x+dir[i][0];
yy=y+dir[i][1];
next=xx*5+yy;
if (judge(xx,yy)==1 &&vis[xx][yy]==0)
{;
mark[next]=s;//这个点若已经访问,则就必然被标记,所以每个点只走一次,然后用并查集储存;
if (xx==4&&yy==4)
return;
vis[xx][yy]=1;
q.push(next);
}
}
}
}
int main ()
{
int n,k;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
scanf("%d",&mapp[i][j]);
}
}
bfs();
print(24);
return 0;
}
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int vis[107][107],mapp[107][107],mark[40];
int sum,xx,yy,next;
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
int judge ( int x, int y)
{
if ( x< 0 || y < 0 || x > 4 || y > 4 )
{
return 0;
}
if ( mapp[x][y] == 1 )
{
return 0;
}
return 1;
}
void print ( int pos )
{
if ( mark[pos] != -1 )
{
print ( mark[pos] );
}
printf("(%d, %d)\n",pos/5,pos%5);
}
void bfs()
{
memset(vis,0,sizeof(vis));
queue<int> q;
mark[0]=-1;
vis[0][0]=1;
int s=0;
q.push(s);
while (!q.empty())
{
s=q.front(); q.pop();
int x=s/5;
int y=s%5;
for (int i=0; i<4; i++)
{
xx=x+dir[i][0];
yy=y+dir[i][1];
next=xx*5+yy;
if (judge(xx,yy)==1 &&vis[xx][yy]==0)
{;
mark[next]=s;//这个点若已经访问,则就必然被标记,所以每个点只走一次,然后用并查集储存;
if (xx==4&&yy==4)
return;
vis[xx][yy]=1;
q.push(next);
}
}
}
}
int main ()
{
int n,k;
for(int i=0; i<5; i++)
{
for(int j=0; j<5; j++)
{
scanf("%d",&mapp[i][j]);
}
}
bfs();
print(24);
return 0;
}