hdu 1242 우선 대기 열 및 비 우선 대기 열 2 중 방법
Problem Description
Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M (N, M <= 200) matrix. There are WALLs, ROADs, and GUARDs in the prison.
Angel's friends want to save Angel. Their task is: approach Angel. We assume that "approach Angel" is to get to the position where Angel stays. When there's a guard in the grid, we must kill him (or her?) to move into the grid. We assume that we moving up, down, right, left takes us 1 unit time, and killing a guard takes 1 unit time, too. And we are strong enough to kill all the guards.
You have to calculate the minimal time to approach Angel. (We can move only UP, DOWN, LEFT and RIGHT, to the neighbor grid within bound, of course.)
Input
First line contains two integers stand for N and M.
Then N lines follows, every line has M characters. "." stands for road, "a" stands for Angel, and "r" stands for each of Angel's friend.
Process to the end of the file.
Output
For each test case, your program should output a single integer, standing for the minimal time needed. If such a number does no exist, you should output a line containing "Poor ANGEL has to stay in the prison all his life."
Sample Input
7 8#.#####.#.a#..r.#..#x.....#..#.##...##...#..............
Sample Output
13
/* : n×m , a, r。# ,. 。 x 。r , , 1 。 x , 1 x。 r a
: 。 。
AC #include<stdio.h> #include<string.h> int a[205][205],s_x,s_y,e_x,e_y,flag[205][205]; int b[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; struct haha { int x; int y; int ti; }que[1000000]; int bfs() { int head=0,tail=1,min=100000000,i; que[1].x=s_x;que[1].y=s_y;que[1].ti=0;flag[s_x][s_y]=1; while(head<tail) { head++; if(que[head].ti>min) continue; if(que[head].x==e_x&&que[head].y==e_y) { if(min>que[head].ti) min=que[head].ti;continue; } for(i=0;i<4;i++) if(a[que[head].x+b[i][0]][que[head].y+b[i][1]]!=0&&flag[que[head].x+b[i][0]][que[head].y+b[i][1]]==0) { flag[que[head].x+b[i][0]][que[head].y+b[i][1]]=1; que[++tail].x=que[head].x+b[i][0]; que[tail].y=que[head].y+b[i][1]; que[tail].ti=que[head].ti+a[que[head].x+b[i][0]][que[head].y+b[i][1]]; } } return min; } int main() { int hang,lie,i,j,ch,ans; while(scanf("%d %d",&hang,&lie)!=EOF) { getchar(); memset(a,0,sizeof(a)); memset(flag,0,sizeof(flag)); for(i=1;i<=hang;i++) { for(j=1;j<=lie;j++) { ch=getchar(); if(ch=='#') a[i][j]=0; if(ch=='.') a[i][j]=1; if(ch=='x') a[i][j]=2; if(ch=='a'){s_x=i;s_y=j;a[i][j]=1;} if(ch=='r'){e_x=i;e_y=j;a[i][j]=1;} } getchar(); } ans=bfs(); if(ans==100000000) printf("Poor ANGEL has to stay in the prison all his life.
"); else printf("%d
",ans); } }
:
#include<stdio.h> #include<string.h> #include<queue> using namespace std; int a[205][205],e_x,e_y; int b[4][2]={{-1,0},{0,-1},{1,0},{0,1}}; struct haha { int x; int y; int ti; bool operator <(const haha &a) const //a { return ti>a.ti;// > < } }q,temp,end;
priority_queue<haha>duilie;// duilie
int bfs() { int i; int tx,ty; while(!duilie.empty()) { temp=duilie.top(); duilie.pop(); for(i=0;i<4;i++) { tx=temp.x+b[i][0]; ty=temp.y+b[i][1]; if(a[tx][ty]!=0) { q.x=tx; q.y=ty; q.ti=temp.ti+a[tx][ty]; duilie.push(q); if(tx==e_x&&ty==e_y) return temp.ti+1; a[tx][ty]=0; } } } return -1; } int main() { int hang,lie,i,j,ch,ans; while(scanf("%d %d",&hang,&lie)!=EOF) { getchar(); memset(a,0,sizeof(a)); while(!duilie.empty()) duilie.pop(); for(i=1;i<=hang;i++) { for(j=1;j<=lie;j++) { ch=getchar(); if(ch=='#') a[i][j]=0; if(ch=='.') a[i][j]=1; if(ch=='x') a[i][j]=2; if(ch=='a') { q.x=i; q.y=j; q.ti=0; duilie.push(q); a[i][j]=1; } if(ch=='r'){e_x=i;e_y=j;a[i][j]=1;} } getchar(); } ans=bfs(); if(ans==-1) printf("Poor ANGEL has to stay in the prison all his life.
"); else printf("%d
",ans); } return 0; }
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.