HDU 1026(Ignatius and the Princess I)解题纠错
2011-03-17 13:51
543 查看
以下代码来自http://topic.csdn.net/u/20110315/15/b4d44d3f-d522-444b-abb9-1ed3d3fd568b.html?seed=2055750136&r=72208694#r_72208694
(1)以下语句中把now.y<m错写为now.x<m:
if(now.x>=0 && now.x<n && now.y>=0 && now.x<m && mark[now.x][now.y])
(2)以下语句只是把新加的排了序,没有把head..flag-1这段数据参与排序:
qsort(&d[flag],rear-flag,sizeof(elem),cmp);
第1个错误可用以下用例揭示:
4 2
.X
21
2X
2.
但第2个错误很难找到用例。
另外,函数prin1可改为void。以下是修改后AC的代码:
]/*1026 Ignatius and the Princess I*/ #include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct { int x; int y; int ntime; int s; }elem; int n,m; elem d[1000000]; char str[110][110]; bool mark[110][110]; int direc[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; int min; int count; bool prin1(int k) { int p; int i; p=d[k].s; if(p==-1){ printf("It takes %d seconds to reach the target position, let me show you the way./n",d[min].ntime-1); return true; } prin1(p); printf("%ds:(%d,%d)->(%d,%d)/n",d[p].ntime,d[p].x,d[p].y,d[k].x,d[k].y); if(str[d[k].x][d[k].y]!='.'){ for(i=d[p].ntime+1;i<d[k].ntime;i++){ printf("%ds:FIGHT AT (%d,%d)/n",i,d[k].x,d[k].y); } } if(k==min) printf("FINISH/n"); } void prin2() { printf("God please help our poor hero./nFINISH/n"); } int cmp(const void *a,const void *b) { return (*(elem *)a).ntime-(*(elem *)b).ntime; } bool bfs() { int head,rear; int i; int flag; elem now; head=0;rear=1; now.x=0; now.y=0; now.ntime=1; now.s=-1; d[head]=now; mark[now.x][now.y]=false; while(head<rear){ flag=rear; for(i=0;i<4;i++){ now.x=d[head].x+direc[i][0]; now.y=d[head].y+direc[i][1]; now.ntime=d[head].ntime; now.s=head; now.ntime++; if(now.x>=0 && now.x<n && now.y>=0 && now.x<m && mark[now.x][now.y]){ if(str[now.x][now.y]=='X') continue; if(now.x==n - 1 && now.y==m-1){ if(str[now.x][now.y]!='.'){ now.ntime+=str[now.x][now.y]-'0'; } d[rear]=now; min=rear; return true; } if(str[now.x][now.y]=='.'){ mark[now.x][now.y]=false; d[rear]=now; rear++; } else { mark[now.x][now.y]=false; now.ntime+=str[now.x][now.y]-'0'; d[rear]=now; rear++; } } } qsort(&d[flag],rear-flag,sizeof(elem),cmp); head++; } return false; } int main() { 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",&str[i][j]); mark[i][j]=true; } getchar(); } if(bfs()){ prin1(min); } else prin2(); } return 0; }
(1)以下语句中把now.y<m错写为now.x<m:
if(now.x>=0 && now.x<n && now.y>=0 && now.x<m && mark[now.x][now.y])
(2)以下语句只是把新加的排了序,没有把head..flag-1这段数据参与排序:
qsort(&d[flag],rear-flag,sizeof(elem),cmp);
第1个错误可用以下用例揭示:
4 2
.X
21
2X
2.
但第2个错误很难找到用例。
另外,函数prin1可改为void。以下是修改后AC的代码:
]#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct { int x; int y; int ntime; int s; }elem; int n,m; elem d[1000000]; char str[110][110]; bool mark[110][110]; int direc[4][2]={{1,0},{0,1},{-1,0},{0,-1}}; int min; int count; void prin1(int k) //bool prin1(int k) { int p; int i; p=d[k].s; if(p==-1){ printf("It takes %d seconds to reach the target position, let me show you the way./n",d[min].ntime-1); return;//return true; } prin1(p); printf("%ds:(%d,%d)->(%d,%d)/n",d[p].ntime,d[p].x,d[p].y,d[k].x,d[k].y); if(str[d[k].x][d[k].y]!='.'){ for(i=d[p].ntime+1;i<d[k].ntime;i++){ printf("%ds:FIGHT AT (%d,%d)/n",i,d[k].x,d[k].y); } } if(k==min) printf("FINISH/n"); } void prin2() { printf("God please help our poor hero./nFINISH/n"); } int cmp(const void *a,const void *b) { return (*(elem *)a).ntime-(*(elem *)b).ntime; } bool bfs() { int head,rear; int i; int flag; elem now; head=0;rear=1; now.x=0; now.y=0; now.ntime=1; now.s=-1; d[head]=now; mark[now.x][now.y]=false; while(head<rear){ flag=rear; for(i=0;i<4;i++){ now.x=d[head].x+direc[i][0]; now.y=d[head].y+direc[i][1]; now.ntime=d[head].ntime; now.s=head; now.ntime++; if(now.x>=0 && now.x<n && now.y>=0 && now.y<m && mark[now.x][now.y]){ if(str[now.x][now.y]=='X') continue; if(now.x==n - 1 && now.y==m-1){ if(str[now.x][now.y]!='.'){ now.ntime+=str[now.x][now.y]-'0'; } d[rear]=now; min=rear; return true; } if(str[now.x][now.y]=='.'){ mark[now.x][now.y]=false; d[rear]=now; rear++; } else { mark[now.x][now.y]=false; now.ntime+=str[now.x][now.y]-'0'; d[rear]=now; rear++; } } } //qsort(&d[flag],rear-flag,sizeof(elem),cmp); head++; qsort(&d[head],rear-head,sizeof(elem),cmp); //head++; } return false; } int main() { 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",&str[i][j]); mark[i][j]=true; } getchar(); } if(bfs()){ prin1(min); } else prin2(); } return 0; }
相关文章推荐
- HDU - 1026 - Ignatius and the Princess I(bfs)
- HDU-1026-Ignatius and the Princess I
- HDU【1026】Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess I(BFS)
- hdu 1026:Ignatius and the Princess I(优先队列 + bfs广搜。ps:广搜AC,深搜超时,求助攻!)
- hdu 1026【Ignatius and the Princess I】
- HDU 1026 Ignatius and the Princess I - BFS + 优先队列
- HDU 1026 Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I (BFS+优先队列)
- HDU 1026 Ignatius and the Princess I (优先队列+BFS(广度优先搜索))
- HDU-1026Ignatius and the Princess I
- Hdu 1026 bfs Ignatius and the Princess I
- HDU 1026 Ignatius and the Princess I BFS 优先队列
- HDU1026——Ignatius and the Princess I(BFS)
- hdu-1026-Ignatius and the Princess I
- hdu 1026 Ignatius and the Princess (广度优先搜索+路径打印)
- HDU 1026 Ignatius and the Princess I BFS打印路径
- HDU 1026 Ignatius and the Princess I(BFS+优先队列)
- HDU - 1028 Ignatius and the Princess III解题报告(线性dp)
- HDU1026 Ignatius and the Princess I【优先搜索】