Topic links
http://acm.hdu.edu.cn/showproblem.php?pid=1242
Water problem, BFS
Code
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace Std;
int n,m,success;
int Stx,sty,ex,ey;
Char map[210][210];
int dx[4]={0,1,0,-1};
int dy[4]={-1,0,1,0};
struct node
{
int x,y,g,cnt;
};
Queue<node> Q;
void BFs ()
{
node s,e;
while (Q.size ())
Q.pop ();
S.x=stx; s.y=sty; s.cnt=0; s.g=0;
Q.push (s); br> while (Q.size ())
{
S=q.front ();
Q.pop ();
if (S.x==ex&&s.y==ey)
{
Success=1;
printf ("%d\n", s.cnt);
return;
}
for (int i=0;i<4;i++)
{
E.x=s.x+dx[i]; e.y=s.y+dy[i];
if (e.x>=0&&e.x<n& &e.y>=0&&e.y<m&&map[e.x][e.y]!= ' # ')
{
if (s.g==0)
{
if (map[e.x][e.y]== ' x '
{
e.g=1;
e.cnt=s.cnt+1;
Q.push (e);
map[e.x][e.y]= ' # ';
}
Else
{
e.g=0;
e.cnt=s.cnt+1;
Q.push (e);
map[e.x][e.y]= ' # ';
}
}
Else
{
s.g=s.g-1;
s.cnt=s.cnt+1;
Q.push (s);
}
}
}
}
}
int main (void)
{
int i,j;
while (scanf ("%d%d", &n,&m) ==2)
{
GetChar ();
for (i=0;i<n;i++)
{
for (j=0;j<m;j++)
{
scanf ("%c", &map[i][j]);
if (map[i][j]== ' R ')
{
Stx=i; Sty=j;
}
else if (map[i][j]== ' a ')
{
Ex=i; Ey=j;
}
}
GetChar ();
}
success=0;
BFS ();
if (!success)
printf ("Poor ANGEL have to stay in the prison all his life.\n");
}
return 0;
}
Hdu-1242-rescue