标签:poj
http://poj.org/problem?id=2312
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 6903 | Accepted: 2336 |
Description
Input
Output
Sample Input
3 4 YBEB EERE SSTE 0 0
Sample Output
8
Source
/** * @author neko01 */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <set> #include <map> using namespace std; typedef long long LL; #define min3(a,b,c) min(a,min(b,c)) #define max3(a,b,c) max(a,max(b,c)) #define pb push_back #define mp(a,b) make_pair(a,b) #define clr(a) memset(a,0,sizeof a) #define clr1(a) memset(a,-1,sizeof a) #define dbg(a) printf("%d\n",a) typedef pair<int,int> pp; const double eps=1e-9; const double pi=acos(-1.0); const int INF=0x3f3f3f3f; const LL inf=(((LL)1)<<61)+5; const int N=305; char s[N][N]; bool vis[N][N]; int n,m; int sx,sy; int dir[4][2]={-1,0,0,-1,1,0,0,1}; struct node{ int x,y,step; }; bool inmap(int x,int y) { return x>=0&&x<n&&y>=0&&y<=m&&s[x][y]!='S'&&s[x][y]!='R'&&!vis[x][y]; } int bfs() { clr(vis); queue<node>q; node cur,next; cur.x=sx; cur.y=sy; cur.step=0; vis[sx][sy]=true; q.push(cur); while(!q.empty()) { cur=q.front(); if(s[cur.x][cur.y]=='T') return cur.step; q.pop(); if(s[cur.x][cur.y]=='B') { next=cur; next.step++; s[cur.x][cur.y]='E'; q.push(next); continue; } for(int i=0;i<4;i++) { int xx=cur.x+dir[i][0]; int yy=cur.y+dir[i][1]; if(inmap(xx,yy)) { next.x=xx,next.y=yy; next.step=cur.step+1; vis[xx][yy]=true; q.push(next); } } } return -1; } int main() { while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; for(int i=0;i<n;i++) { scanf("%s",s[i]); for(int j=0;j<m;j++) if(s[i][j]=='Y') sx=i,sy=j; } printf("%d\n",bfs()); } return 0; }
/** * @author neko01 */ //#pragma comment(linker, "/STACK:102400000,102400000") #include <cstdio> #include <cstring> #include <string.h> #include <iostream> #include <algorithm> #include <queue> #include <vector> #include <cmath> #include <set> #include <map> using namespace std; typedef long long LL; #define min3(a,b,c) min(a,min(b,c)) #define max3(a,b,c) max(a,max(b,c)) #define pb push_back #define mp(a,b) make_pair(a,b) #define clr(a) memset(a,0,sizeof a) #define clr1(a) memset(a,-1,sizeof a) #define dbg(a) printf("%d\n",a) typedef pair<int,int> pp; const double eps=1e-9; const double pi=acos(-1.0); const int INF=0x3f3f3f3f; const LL inf=(((LL)1)<<61)+5; const int N=305; char s[N][N]; bool vis[N][N]; int n,m; int sx,sy; int dir[4][2]={-1,0,0,-1,1,0,0,1}; struct node{ int x,y,step; bool operator < (const node &p) const{ return step>p.step; } }; bool inmap(int x,int y) { return x>=0&&x<n&&y>=0&&y<=m&&s[x][y]!='S'&&s[x][y]!='R'&&!vis[x][y]; } int bfs() { clr(vis); priority_queue<node>q; node cur,next; cur.x=sx; cur.y=sy; cur.step=0; vis[sx][sy]=true; q.push(cur); while(!q.empty()) { cur=q.top(); if(s[cur.x][cur.y]=='T') return cur.step; q.pop(); for(int i=0;i<4;i++) { int xx=cur.x+dir[i][0]; int yy=cur.y+dir[i][1]; if(inmap(xx,yy)) { next.x=xx,next.y=yy; if(s[xx][yy]=='B') next.step=cur.step+2; else next.step=cur.step+1; vis[xx][yy]=true; q.push(next); } } } return -1; } int main() { while(~scanf("%d%d",&n,&m)) { if(n==0&&m==0) break; for(int i=0;i<n;i++) { scanf("%s",s[i]); for(int j=0;j<m;j++) if(s[i][j]=='Y') sx=i,sy=j; } printf("%d\n",bfs()); } return 0; }
标签:poj
原文地址:http://blog.csdn.net/neko01/article/details/39997673