HDU1401 BFS
2013-02-14 21:36
211 查看
题意:有上一状态到下一状态
单向bfs
就是每次只改变一个点的一个方向!!!
( 参考。。。http://blog.csdn.net/cscj2010/article/details/7371482)
View Code
单向bfs
就是每次只改变一个点的一个方向!!!
( 参考。。。http://blog.csdn.net/cscj2010/article/details/7371482)
View Code
#include<stdio.h> #include<string.h> #include<stdlib.h> #include<queue> #include<algorithm> using namespace std; const int maxn = 8; bool vis[ maxn ][ maxn ][ maxn ][ maxn ][ maxn ][ maxn ][ maxn ][ maxn ]; bool mat[ maxn+1 ][ maxn+1 ]; struct node{ int x,y; }a[ maxn ]; struct node2{ int x[4],y[4],t; }p,pp; void init(){ memset( mat,false,sizeof( mat )); } const int dx[]={0,0,1,-1}; const int dy[]={1,-1,0,0}; bool ok( node2 p ){ for( int i=0;i<4;i++ ){ if( mat[ p.x[i] ][ p.y[i] ]==false ) return false; } return true; } bool empty( node2 pp,int i ){ for( int k=0;k<4;k++ ){ if( i!=k ){ if( pp.x[i]==pp.x[k]&&pp.y[i]==pp.y[k] ){ return false; } } } return true; } int bfs(){ for( int i=0;i<4;i++ ){ p.x[i]=a[i].x; p.y[i]=a[i].y; // p.t=0; } p.t=0; if( ok(p)==true ) return p.t; queue<node2>q; q.push( p ); memset( vis,false,sizeof( vis )); vis[ p.x[0] ][ p.y[0] ][ p.x[1] ][ p.y[1] ][ p.x[2] ][ p.y[2] ][ p.x[3] ][ p.y[3] ]=true; while( !q.empty() ){ p=q.front(),q.pop(); if( p.t>=8 ) return -11; if( ok(p)==true ) return p.t; for( int i=0;i<4;i++ ){ for( int j=0;j<4;j++ ){//dir pp=p; pp.x[ i ]=p.x[ i ]+dx[ j ]; pp.y[ i ]=p.y[ i ]+dy[ j ]; pp.t=p.t+1; if( pp.x[0]<0||pp.x[0]>=8||pp.y[0]<0||pp.y[0]>=8||pp.x[1]<0||pp.x[1]>=8||pp.y[1]<0||pp.y[1]>=8 ) continue; if( pp.x[2]<0||pp.x[2]>=8||pp.y[2]<0||pp.y[2]>=8||pp.x[3]<0||pp.x[3]>=8||pp.y[3]<0||pp.y[3]>=8 ) continue; if( vis[ pp.x[0] ][ pp.y[0] ][ pp.x[1] ][ pp.y[1] ][ pp.x[2] ][ pp.y[2] ][ pp.x[3] ][ pp.y[3] ]==true ) continue; if( empty( pp,i )==true ){ vis[ pp.x[0] ][ pp.y[0] ][ pp.x[1] ][ pp.y[1] ][ pp.x[2] ][ pp.y[2] ][ pp.x[3] ][ pp.y[3] ]=true; if( ok(pp)==true ) return pp.t; q.push( pp ); } else{ pp.x[i]+=dx[j]; pp.y[i]+=dy[j]; if( pp.x[0]>=0&&pp.x[0]<8&&pp.y[0]>=0&&pp.y[0]<8&&pp.x[1]>=0&&pp.x[1]<8&&pp.y[1]>=0&&pp.y[1]<8&&pp.x[2]>=0&&pp.x[2]<8&&pp.y[2]>=0&&pp.y[2]<8&&pp.x[3]>=0&&pp.x[3]<8&&pp.y[3]>=0&&pp.y[3]<8 ){ if( empty( pp,i )==true&&vis[ pp.x[0] ][ pp.y[0] ][ pp.x[1] ][ pp.y[1] ][ pp.x[2] ][ pp.y[2] ][ pp.x[3] ][ pp.y[3] ]==false ){ vis[ pp.x[0] ][ pp.y[0] ][ pp.x[1] ][ pp.y[1] ][ pp.x[2] ][ pp.y[2] ][ pp.x[3] ][ pp.y[3] ]=true; if( ok(pp)==true ) return pp.t; q.push( pp ); } } } } } } return -11; } int main(){ while( scanf("%d%d",&a[0].x,&a[0].y)==2 ){ a[0].x--,a[0].y--; for( int i=1;i<4;i++ ){ scanf("%d%d",&a[i].x,&a[i].y); a[i].x--,a[i].y--; } init(); int x,y; for( int i=0;i<4;i++ ){ scanf("%d%d",&x,&y); x--,y--; mat[x][y]=true; } int flag=bfs( ); if( flag!=-11 ) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- hdu1401 Solitaire ----双向BFS
- HDU1401 双广BFS
- hdu1401 Solitaire (双向bfs)
- HDU1401:Solitaire(BFS)
- Hdu1401-Solitaire(双向bfs)
- HDU1401 BFS
- 【数据结构】图的遍历之DFS和BFS
- POJ2308 Dearboy's Puzzle(DFS+BFS)
- HDU 2444 The Accomodation of Students (二分图匹配+匈牙利算法+bfs判断是否为二分图)
- 【数据结构与算法】自己动手实现图的BFS和DFS(附完整源码)
- 广度优先搜索BFS(迷宫问题)
- (BFS)A strange lift--HDOJ
- HDU 1240 Asteroids!(BFS)
- HDU 1728 逃离迷宫(DFS||BFS)
- UVA - 1600 Patrol Robot(BFS 三维判重)
- BFS --- 模板题
- ACM_程序设计竞赛:穷举法:BFS(广度优先)
- 基础数据结构算法_DFS and BFS
- 一天一道算法题--5.25--bfs或者最短路
- hdoj 1242 Rescue (bfs 优先队列)