标签:
Description
Input
Output
Sample Input
Sample Output
Hint
第一个样例
次数 船 方向 左岸 右岸(狼 羊)
0: 0 0 3 3 0 0
1: 2 0 > 1 3 2 0
2: 1 0 < 2 3 1 0
3: 2 0 > 0 3 3 0
4: 1 0 < 1 3 2 0
5: 0 2 > 1 1 2 2
6: 1 1 < 2 2 1 1
7: 0 2 > 2 0 1 3
8: 1 0 < 3 0 0 3
9: 2 0 > 1 0 2 3
10: 1 0 < 2 0 1 3
11: 2 0 > 0 0 3 3
乍一看以为是dp,其实看数据范围是一道bfs搜索,里面有各种简单的剪枝,vis[i][j][d]表示船上有i只羊,j只狼,d表示船的方向,0表示开始,1表示对岸。
代码:
#include<cstdio> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #include<string> #include<cmath> #include<queue> #include<vector> #include<map> #include<set> #define INF 0x3f3f3f3f #define mem(a,b) memset(a,b,sizeof(a)) using namespace std; const int maxd=200+5; #define lson l,m,rt<<1 #define rson m+1,r,rt<<1 | 1 typedef long long ll; typedef pair<int,int> pii; //--------------------------- int dx[]= {0,0,1,-1}; int dy[]= {1,-1,0,0}; typedef struct node { int x,y,d,t; node(int x_ = 0, int y_ = 0,int d_=0, int t_ = 0) { x = x_; y = y_; d = d_; t = t_; } }; int vis[maxd][maxd][3]; int x,y,n; int bfs(int a,int b,int d,int t) { queue<node> q; node tmp(a,b,d,t); q.push(tmp); vis[tmp.x][tmp.y][0]=1; while(!q.empty()) { node now=q.front(); q.pop(); if(now.x==x && now.y==y && now.d==1) { return now.t; } for(int i=0; i<=now.x; ++i) ///sheep for(int j=0; j<=now.y; ++j) ///wolf { if(i+j==0 ) continue; if((i<j) && i) continue; if(i+j>n) continue; if((now.x-i<now.y-j) && (now.x-i)) continue; if((x-now.x+i < y-now.y+j) && (x-now.x+i)) continue; if(vis[x-now.x+i][y-now.y+j][!now.d]) continue; node tmp(x-now.x+i,y-now.y+j,!now.d,now.t+1); q.push(tmp); vis[tmp.x][tmp.y][tmp.d]=1; } } return -1; } int main() { freopen("1.txt","r",stdin); while( scanf("%d%d%d",&x,&y,&n)!=EOF) { mem(vis,0); int ans=bfs(x,y,0,0); printf("%d\n",ans); } return 0; }
标签:
原文地址:http://blog.csdn.net/whoisvip/article/details/45580419