ACM对拍造数据
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1253
四个程序:
错误程序
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<queue>
using namespace std;
const int maxn=60;
int mp[maxn][maxn][maxn];
int st[maxn][maxn][maxn];
//int f[6][3]= {{1,0,0},{0,1,0},{-1,0,0},{0,-1,0},{0,0,1},{0,0,-1}};
int tx[] = {1,-1,0,0,0,0};
int ty[] = {0,0,1,-1,0,0};
int tz[] = {0,0,0,0,1,-1};
int t,T,n,a,b,c,flag;
int abs(int x){
return x>=0?x:-x;
}
struct Node
{
int x,y,z,step;
};
queue<Node >Q;
void bfs(int x,int y,int z) {
Node p,q;
if(!Q.empty()) Q.pop();
p.x=x;
p.y=y;
p.z=z;
p.step=0;
mp[x][y][z]=1;
Q.push(p);
while(!Q.empty()) {
p=Q.front();
Q.pop();
for(int i=0; i<6; i++) {
q=p;
q.x+=tx[i];
q.y+=ty[i];
q.z+=tz[i];
if(q.x<0||q.y<0||q.z<0||q.x>=a||q.y>=b||q.z>=c||mp[q.x][q.y][q.z]==1) {
continue;
}
q.step++;
if(q.x==a-1&&q.y==b-1&&q.z==c-1&&q.step<=t) {
flag=1;
t=q.step;
break;
}
mp[q.x][q.y][q.z]=1;
if(abs(q.x-a+1)+abs(q.y-b+1)+abs(q.z-c+1)+q.step>t+2)//由于行走只能朝6个固定方向,这里是对剩下时间里能否走到出口进行预判,如果走最短路径依然不能再规定时间内到达出口,明显是不行的,当然不加这个判断也能AC,只是比较消耗时间
continue;
Q.push(q);
}
if(flag)break;
}
}
int main() {
freopen("1.txt","r",stdin);//读入数据
freopen("1.out","w",stdout); //输出数据
scanf("%d",&T);
while(T--) {
flag=0;
memset(mp,0,sizeof(mp));
scanf("%d%d%d%d",&a,&b,&c,&t);
for(int i=0; i<a; i++) {
for(int j=0; j<b; j++) {
for(int k=0; k<c; k++) {
scanf("%d",&mp[i][j][k]);
}
}
}
memset(st,0,sizeof(st));
if(a+b+c==3){
printf("0\n");
continue;
}
bfs(0,0,0);
if(flag) {
printf("%d\n",t);
} else {
printf("-1\n");
}
}
}
网上的标程
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
const int N = 55;
int map[N][N][N];
int vis[N][N][N];
int tx[] = {1,-1,0,0,0,0};
int ty[] = {0,0,1,-1,0,0};
int tz[] = {0,0,0,0,1,-1};
int a,b,c,t,ans;
struct Node
{
int x,y,z,step;
};
int abs(int x)//绝对值
{
return x<0?-x:x;
}
int check(int i,int j,int k)//判断是否可行
{
if(i<0 || j<0 || k<0 || i>=a || j>=b || k>=c || map[i][j][k])
return 0;
return 1;
}
int bfs(int x,int y,int z)
{
int i;
queue<Node> Q;
Node p,q;
p.x = x;
p.y = y;
p.z = z;
p.step = 0;
vis[x][y][z] = 1;
Q.push(p);
while(!Q.empty())
{
p = Q.front();
Q.pop();
if(p.x == a-1 && p.y == b-1 && p.z==c-1 && p.step<=t)
return p.step;
for(i = 0;i<6;i++)
{
q = p;
q.x+=tx[i];
q.y+=ty[i];
q.z+=tz[i];
if(!vis[q.x][q.y][q.z] && check(q.x,q.y,q.z))
{
q.step++;
vis[q.x][q.y][q.z] = 1;
if(abs(q.x-a+1)+abs(q.y-b+1)+abs(q.z-c+1)+q.step>t)//由于行走只能朝6个固定方向,这里是对剩下时间里能否走到出口进行预判,如果走最短路径依然不能再规定时间内到达出口,明显是不行的,当然不加这个判断也能AC,只是比较消耗时间
continue;
Q.push(q);
}
}
}
return -1;
}
int main()
{ freopen("1.txt","r",stdin);//读入数据
freopen("2.out","w",stdout); //输出数据
int cas;
scanf("%d",&cas);
while(cas--)
{
int i,j,k;
scanf("%d%d%d%d",&a,&b,&c,&t);
memset(map,0,sizeof(map));
memset(vis,0,sizeof(vis));
for(i = 0;i<a;i++)
for(j = 0;j<b;j++)
for(k = 0;k<c;k++)
scanf("%d",&map[i][j][k]);
ans = bfs(0,0,0);
printf("%d\n",ans);
}
return 0;
}
造数据的程序:
#include<iostream>
#include<cstdlib>
#include<ctime>
#define random(a,b) (rand()%(b-a+1)+a)
using namespace std;
int a,b,c,t,p;
int main()
{
freopen("1.txt","w",stdout);//输出文件
srand((unsigned)time(NULL));//数据随机化时间种子
p=random(1,10);
printf("%d\n",p);
for(int q=1;q<=p;q++)
{
a=random(1,10);b=random(1,10);c=random(1,10);t=random(1,100);
printf("%d %d %d %d\n",a,b,c,t);
for(int i=1;i<=a;i++)
for(int j=1;j<=b;j++)
{
for(int k=1;k<=c;k++) printf("%d ",random(0,1));
printf("\n");
}
}
return 0;
}
.bat文件
:loop //标签
3 //打开造数据的eve
1 //打开1.exe
2 //打开2.exe
fc 1.out 2.out //比较两个out
if errorlevel==1 pause //如果不相等就停止
goto loop //继续循环
结果:
出错误了
最后发现是Q队列没有清空
风在前,无惧!