poj 3984

时间:2024-04-14 21:02:35

http://poj.org/problem?id=3984

题目很简单,就是简单的BFS吧,主要的难点在于坐标的问题

这个呢,可以反其道而行之,就是你从(1,1)到(5,5),你肯定走过一次

走过一次呢,那么路径肯定也标记过,那么你完全就可以从(5,5)到(1,1)按标记的记号走一次,那样就是最短的路径了

 #include <stdio.h>
#include <iostream>
#include <queue>
#include <string.h> using namespace std; queue<int>first;
queue<int>second; int a[][],b[][],ans; struct {
int x,y;
}s[]; bool mark[][]; void bfs(int x,int y)
{
int he,ba;
first.push(x);
second.push(y);
while(!first.empty())
{
he=first.front();
first.pop();
ba=second.front();
second.pop();
//printf("%d %d\n",he,ba);
if(he==&&ba==) break;
if(a[he][ba+]==&&mark[he][ba+])
{
first.push(he);
second.push(ba+);
b[he][ba+]=b[he][ba]+;
mark[he][ba+]=false;
}
if(a[he+][ba]==&&mark[he+][ba])
{
first.push(he+);
second.push(ba);
b[he+][ba]=b[he][ba]+;
mark[he+][ba]=false;
}
}
} void findroad(int x,int y)
{
if(x==&&y==) return;
if(!mark[x][y-]){
ans--;
s[ans].x=x;
s[ans].y=y-;
findroad(x,y-);
}
if(!mark[x-][y])
{
ans--;
s[ans].x=x-;
s[ans].y=y;
findroad(x-,y);
}
} int main()
{
memset(a,,sizeof(a));
memset(mark,true,sizeof(mark));
memset(b,,sizeof(b));
for(int i=;i<=;i++)
for(int j=;j<=;j++)
scanf("%d",&a[i][j]);
s[].x=;
s[].y=;
bfs(,);
ans=b[][];
s[ans].x=;
s[ans].y=;
findroad(,);
for(int i=;i<=b[][];i++)
printf("(%d, %d)\n",s[i].x-,s[i].y-);
return ;
}