您的位置:首页 > 其它

poj3414 倒水问题

2017-09-13 14:44 260 查看
经典搜索题

第一次没有重置v数组

第二次写了两个DROP(2)

wa了半天

对拍拍了半个小时…

【顺便复习了对拍??

真的是bug体质…



#include<iostream>
#include<cmath>
#include<string.h>
#define maxn 130
using namespace std;
template <typename T> void read(T &x){
x=0;int f=1;char ch=getchar();
for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
for(;isdigit(ch);ch=getchar())x=(x<<1)+(x<<3)+ch-'0';
x*=f;
}

typedef long long LL;

int a,b,c;
bool flag;
struct node{
int k1,k2;
int op;
int step;
int pre;
}num[maxn*maxn];
bool v[maxn][maxn];
int head,tail;

void moving(int k){
if(k==1){
if(!v[a][num[head].k2]){
tail++;
num[tail].k1=a;
num[tail].k2=num[head].k2;
num[tail].op=k;
num[tail].step=num[head].step+1;
num[tail].pre=head;
v[a][num[head].k2]=1;
}
}
if(k==2){
if(!v[num[head].k1][b]){
tail++;
num[tail].k1=num[head].k1;
num[tail].k2=b;
num[tail].op=k;
num[tail].step=num[head].step+1;
num[tail].pre=head;
v[num[head].k1][b]=1;
}
}
if(k==3){
if(!v[0][num[head].k2]){
tail++;
num[tail].k1=0;
num[tail].k2=num[head].k2;
num[tail].op=k;
num[tail].step=num[head].step+1;
num[tail].pre=head;
v[0][num[head].k2]=1;
}
}
if(k==4){
if(!v[num[head].k1][0]){
tail++;
num[tail].k1=num[head].k1;
num[tail].k2=0;
num[tail].op=k;
num[tail].step=num[head].step+1;
num[tail].pre=head;
v[num[head].k1][0]=1;
}
}
if(k==5){
if(num[head].k1+num[head].k2<=b){
if(!v[0][num[head].k1+num[head].k2]){
tail++;
num[tail].k1=0;
num[tail].k2=num[head].k1+num[head].k2;
num[tail].op=k;
num[tail].step=num[head].step+1;
num[tail].pre=head;
v[0][num[head].k1+num[head].k2]=1;
}
}
else{
if(!v[num[head].k1+num[head].k2-b][b]){
tail++;
num[tail].k1=num[head].k1+num[head].k2-b;
num[tail].k2=b;
num[tail].op=k;
num[tail].step=num[head].step+1;
num[tail].pre=head;
v[num[head].k1+num[head].k2-b][b]=1;
}
}
}
if(k==6){
if(num[head].k1+num[head].k2<=a){
if(!v[num[head].k1+num[head].k2][0]){
tail++;
num[tail].k1=num[head].k1+num[head].k2;
num[tail].k2=0;
num[tail].op=k;
num[tail].step=num[head].step+1;
num[tail].pre=head;
v[num[head].k1+num[head].k2][0]=1;
}
}
else{
if(!v[a][num[head].k1+num[head].k2-a]){
tail++;
num[tail].k1=a;
num[tail].k2=num[head].k1+num[head].k2-a;
num[tail].op=k;
num[tail].step=num[head].step+1;
num[tail].pre=head;
v[a][num[head].k1+num[head].k2-a]=1;
}
}
}
}

int bfs(){
head=0,tail=1;
num[1].k1=0,num[1].k2=0,num[1].op=0,num[1].pre=0,num[1].step=0;
memset(v,0,sizeof(v));
v[0][0]=1;
while(head<tail){
head++;
if(num[head].k1==c||num[head].k2==c){
flag=1;
return head;
}
for(int i=1;i<=6;++i){
//i==1 FILL(1)
//i==2 FILL(2)
//i==3 drop(1)
//i==4 drop(2)
//i==5 pour(1,2)
//i==6 pour(2,1)
moving(1);
moving(2);
moving(3);
moving(4);
moving(5);
moving(6);
}
}
}

int main(){
read(a),read(b),read(c);
int id[maxn*maxn];
memset(id,0,sizeof(id));
LL n=bfs();
if(!flag) cout<<"impossible"<<endl;
else{
cout<<num
.step<<endl;
int step=num
.step;
id[step]=n;
for(int i=step-1;i>=1;--i){
id[i]=num[id[i+1]].pre;
}
for(int i=1;i<=step;++i){
if(num[id[i]].op==1) cout<<"FILL(1)"<<endl;
if(num[id[i]].op==2) cout<<"FILL(2)"<<endl;
if(num[id[i]].op==3) cout<<"DROP(1)"<<endl;
if(num[id[i]].op==4) cout<<"DROP(2)"<<endl;
if(num[id[i]].op==5) cout<<"POUR(1,2)"<<endl;
if(num[id[i]].op==6) cout<<"POUR(2,1)"<<endl;
}
}
return 0;
}


仿佛是第三次写对拍了:

@echo off

:loop

poj3414rand.exe > data.in

poj3414std.exe < data.in > std.out

poj3414.exe < data.in > myprogram.out

fc myprogram.out std.out

if not errorlevel 1 goto loop

pause

goto loop

找到标程或手写暴力

再写一个rand.cpp

先生成小数据

再在桌面生成txt

另存为.bat批处理文件

将三个exe和批处理文件放在同一目录下

点击批处理文件即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: