标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4122 Accepted Submission(s): 1245
#include<stdio.h> #include<queue> #include<iostream> #include <string.h> #include <algorithm> #include <map> using namespace std; typedef long long LL; struct Node{ int x[4],y[4]; int step; }s,t; int graph[10][10]; bool vis[8][8][8][8][8][8][8][8]; bool equals(Node a){ ///这个地方开始想错了,以为是每一个点的状态对应下一行每一行的状态,结果是只要能到达目标 ///状态就OK,比如说是起始地第二个点到达目标的第一个点 for(int i=0;i<4;i++){ if(!graph[a.x[i]][a.y[i]]) return false; } return true; } bool check(Node a){ for(int i=0;i<4;i++){ if(a.x[i]<0||a.x[i]>=8||a.y[i]<0||a.y[i]>=8) return false; } if(vis[a.x[0]][a.y[0]][a.x[1]][a.y[1]][a.x[2]][a.y[2]][a.x[3]][a.y[3]]) return false; return true; } bool cango(Node next,int k){ for(int i=0;i<4;i++){ if(i!=k&&next.x[i]==next.x[k]&&next.y[i]==next.y[k]) return false; } return true; } int dir[][2] = {{1,0},{-1,0},{0,1},{0,-1}}; bool bfs(){ queue<Node> q; q.push(s); s.step = 0; while(!q.empty()){ Node now = q.front(); q.pop(); if(now.step>8){ return false; } if(equals(now)){ return true; } for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ Node next = now; next.x[i] = now.x[i]+dir[j][0]; next.y[i] = now.y[i]+dir[j][1]; next.step = now.step+1; if(next.step>8) continue; ///大神的剪枝是>=8 if(!check(next)) continue; if(cango(next,i)){ if(equals(next)) return true; vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] = true; q.push(next); } else{ next.x[i] = now.x[i]+2*dir[j][0]; next.y[i] = now.y[i]+2*dir[j][1]; if(!check(next)) continue; if(cango(next,i)){ if(equals(next)) return true; vis[next.x[0]][next.y[0]][next.x[1]][next.y[1]][next.x[2]][next.y[2]][next.x[3]][next.y[3]] = true; q.push(next); } } } } } return false; } int main() { while(scanf("%d%d",&s.x[0],&s.y[0])!=EOF){ memset(graph,0,sizeof(graph)); s.x[0]-=1,s.y[0]-=1; for(int i=1;i<4;i++){ scanf("%d%d",&s.x[i],&s.y[i]); s.x[i]--; s.y[i]--; } for(int i=0;i<4;i++){ scanf("%d%d",&t.x[i],&t.y[i]); t.x[i]--; t.y[i]--; graph[t.x[i]][t.y[i]] = 1; } memset(vis,false,sizeof(vis)); vis[s.x[0]][s.y[0]][s.x[1]][s.y[1]][s.x[2]][s.y[2]][s.x[3]][s.y[3]] = true; bool flag = bfs(); if(flag) printf("YES\n"); else printf("NO\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/liyinggang/p/5592638.html