【POJ 2044】 Weather Forecast
【题目链接】
http://poj.org/problem?id=2044
【算法】
广度优先搜索
【代码】
#include <algorithm> #include <bitset> #include <cctype> #include <cerrno> #include <clocale> #include <cmath> #include <complex> #include <cstdio> #include <cstdlib> #include <cstring> #include <ctime> #include <deque> #include <exception> #include <fstream> #include <functional> #include <limits> #include <list> #include <map> #include <iomanip> #include <ios> #include <iosfwd> #include <iostream> #include <istream> #include <ostream> #include <queue> #include <set> #include <sstream> #include <stdexcept> #include <streambuf> #include <string> #include <utility> #include <vector> #include <cwchar> #include <cwctype> #include <stack> #include <limits.h> using namespace std; #define MAXN 400 const int dx[9] = {0,-1,-2,0,0,1,2,0,0}; const int dy[9] = {0,0,0,-1,-2,0,0,1,2}; int i,j,k,n; int a[MAXN][5][5]; bool visited[5][5][MAXN][7][7][7][7]; struct info { int x,y,day; int s1,s2,s3,s4; }; inline bool check(int x,int y,int day,int s1,int s2,int s3,int s4) { if (x <= 0 || x > 3 || y <= 0 || y > 3) return false; if (a[day][x][y] == 1) return false; if (a[day][x][y+1] == 1) return false; if (a[day][x+1][y] == 1) return false; if (a[day][x+1][y+1] == 1) return false; if (s1 >= 7 || s2 >= 7 || s3 >= 7 || s4 >= 7) return false; return true; } inline bool bfs() { int i,sa,sb,sc,sd,tx,ty; queue< info > q; info cur; if (a[1][2][2] || a[1][2][3] || a[1][3][2] || a[1][3][3]) return false; memset(visited,false,sizeof(visited)); while (!q.empty()) q.pop(); q.push((info){2,2,1,1,1,1,1}); visited[2][2][1][1][1][1][1] = true; while (!q.empty()) { cur = q.front(); q.pop(); if (cur.day == n) return true; for (i = 0; i < 9; i++) { tx = cur.x + dx[i]; ty = cur.y + dy[i]; sa = cur.s1 + 1; sb = cur.s2 + 1; sc = cur.s3 + 1; sd = cur.s4 + 1; if (tx == 1 && ty == 1) sa = 0; if (tx == 1 && ty == 3) sb = 0; if (tx == 3 && ty == 1) sc = 0; if (tx == 3 && ty == 3) sd = 0; if (check(tx,ty,cur.day+1,sa,sb,sc,sd) && !visited[tx][ty][cur.day+1][sa][sb][sc][sd]) { q.push((info){tx,ty,cur.day+1,sa,sb,sc,sd}); visited[tx][ty][cur.day+1][sa][sb][sc][sd] = true; } } } return false; } int main() { while (scanf("%d",&n) != EOF && n) { for (i = 1; i <= n; i++) { for (j = 1; j <= 4; j++) { for (k = 1; k <= 4; k++) { scanf("%d",&a[i][j][k]); } } } if (bfs()) printf("%d\n",1); else printf("%d\n",0); } return 0; }