nyoj 635 Oh, my goddess
Oh, my goddess
时间限制:3000 ms | 内存限制:65535 KB
难度:3
描述
Shining Knight is the embodiment of justice and he has a very sharp sword can even cleavewall. Many bad guys are dead on his sword.
One day, two evil sorcerer cgangee and Jackchess decided to give him some colorto see. So they kidnapped Shining Knight's beloved girl--Miss Ice! They built a M x Nmaze with magic and shut her up in it.
Shining Knight arrives at the maze entrance immediately. He can reach any adjacent emptysquare of four directions -- up, down, left, and right in 1 second. Or cleave one adjacent wall in 3
seconds, namely,turn it into empty square. It's the time to save his goddess! Notice: ShiningKnight won't leave the maze before he find Miss Ice.
输入
The input consists of blocks of lines. There is a blank line between two blocks.
The first line of each block contains two positive integers M <= 50 and N <= 50separated by one space. In each of the next M lines there is a string of length N contentsO and #.
O represents empty squares. # means a wall.
At last, the location of Miss Ice, ( x, y ). 1 <= x <= M, 1 <= y <= N.
(Shining Knight always starts at coordinate ( 1, 1 ). Both Shining and Ice's locationguarantee not to be a wall.)
输出
The least amount of time Shining Knight takes to save hisgoddess in one line.
样例输入
3 5
O####
#####
#O#O#
3 4
样例输出
14
#include<iostream> #include<algorithm> #include<queue> #include<cstdio> #include<cstring> using namespace std; char f[52][52]; bool vis[52][52]; int Ex,Ey,N,M; const int dir[4][2]={-1,0,1,0,0,1,0,-1}; struct Data { int time; int x,y; }; struct cmp { bool operator() (Data a,Data b) { return a.time>b.time; } }; priority_queue<Data,vector<Data>,cmp>Q; void BFS() { while(!Q.empty())Q.pop(); memset(vis,0,sizeof(vis)); Data t,elem; t.time=0;t.x=t.y=1;Q.push(t); int nx,ny;vis[1][1]=true; while(!Q.empty()) { t=Q.top();Q.pop(); if(t.x==Ex&&t.y==Ey){printf("%d\n",t.time);break;} for(int i=0;i<4;i++) { nx=t.x+dir[i][0]; ny=t.y+dir[i][1]; if(nx>0&&nx<=N&&ny>0&&ny<=M&&!vis[nx][ny]) { elem.time=t.time+(f[nx][ny]=='#'?4:1); elem.x=nx;elem.y=ny; Q.push(elem); vis[nx][ny]=true; } } } } int main() { while(~scanf("%d%d",&N,&M)) { for(int i=1;i<=N;i++) scanf("%s",f[i]+1); scanf("%d%d",&Ex,&Ey); BFS(); } }