您的位置:首页 > 其它

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

]/*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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: