POJ2044 weather forecast --- state rules

Title: Weather Forecast

URL: http://poj.org/problem?id=2044
you can control is a god of rain.
You are a merciful God, hoping to land in peacetime can have enough rain, go to the market and the holidays can be full of sunshine.
You are responsible for control of the weather conditions in a village.
The village as a 4 x 4 grid-like distribution, within each region villages are numbered as shown below:
image

You have a 2 cloud the size of 2 x, this piece is not to cloud outside the village.

You will receive a fair period of time for each area of ​​the village and holiday schedule.

On the first day this time, the central region (6-7-10-11) will rain.

In each successive day, you can select among the four basic directions (truck) in one direction, moving a cloud or two squares, or to remain in the same position.

It does not allow diagonal movement.
Any region can not be more than seven consecutive days or no rainfall time.

Rainy day situation other than this time you do not need any consideration.

Input Format

Enter multiple sets of test cases.

For each test, the first row contains an integer N, represents the number of days during this time.

Took over N lines, depicting the next N days of the fair and festival schedule, the i-th row represents the i-day schedule.

This in N rows, each row containing 16 digits (0 or 1), 0 represents a normal day, and 1 represents a fair day holidays, the i-th digit represents the specific case of the i-th area.
Separated by a space between each row of numbers.
When the input test case N = 0, the input is terminated, and the use case without treatment.

Output Format

Each test case output a integer of 0 or 1, can guarantee that the entire time, where the rain rain, less than in the wrong place, 1 is output.
If it can not guarantee the output 0, and each result row.

data range

1≤N≤365

Sample input:
1
0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0
7
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 0 1 0 0 0 0 1 1 0 0 1
0 0 0 0 0 0 0 0 1 0 0 0 0 1 0 1
0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0
0 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1
0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0
7
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 1 0 0 0 0 1 0 0 0 0 0 1 0 0
0 0 0 1 0 0 0 0 0 0 1 0 1 0 0 0
0 1 0 0 0 0 0 1 0 0 0 0 1 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 1 1 0 1 0 0 0 0 1
0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0
15
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0
0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
1 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0
0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0
1 0 0 1 1 0 0 0 0 1 0 1 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0
0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 0
0
Sample output:
0
1
0
1

This question is in fact a lot of the details.

First, determine whether each region for seven consecutive days no watering, in fact, can be judged by the four corners;

If you define the coordinates of the current status for the cloud, then, when the state in which the four corners of discrepancies meaning of the questions, difficult to represent;

If the state is a case where the coordinates of the four corners, and, then, for the same state, in which case different numbers of days are different;

In summary, the definition: state (time, (x, y), (d1, d2, d3, d4));

Thus, for such a condition is satisfied uniqueness, that is, to ensure that this condition can be accurately describe the state of the case;

Seven consecutive days, so to be safe, we octal compressed state (d1, d2, d3, d4 ), i.e., d1 + d2 * 8 + d3 * 64 + d4 * 512;
rest, to pay attention to detail;
code as follows:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<map>
using namespace std;
struct state
{
	int turn, pos, code;
	state (int x, int y, int z) : turn(x), pos(y), code(z) {}
};
const int dx[10] = {-1, 1, 0, 0, -2, 2, 0, 0}, dy[10] = {0, 0, -1, 1, 0, 0, -2, 2};
bool day[366][17], vis[366][17][4096];
int n;
bool valid(state p)
{	
	if(vis[p.turn][p.pos][p.code]) return false;
	for(int i = 0; i < 4; ++ i)
	{
		if(p.code % 8 >= 7) return false;
		p.code >>= 3;
	}
	if(day[p.turn][p.pos] || day[p.turn][p.pos + 1] || day[p.turn][p.pos + 4] || day[p.turn][p.pos + 5]) return false;
	return true;
}
bool valid(int x, int y)
{
	if(x < 0 || x > 2 || y < 0 || y > 2) return false;
	//be aware of it : The range is [0, 2] !! 
	return true;
}
bool bfs()
{
	queue <state> Q;
	int x, y;
	int p[4] = {}, tmp = 0;
	while(!Q.empty()) Q.pop();
	memset(vis, false, sizeof(vis));
	if(!valid(state(1, 6, 1 + 8 + 64 + 512))) return false;
	vis[1][6][1 + 8 + 64 + 512] = true;
	Q.push(state(1, 6, 1 + 8 + 64 + 512));
	// The coodinate must be transfered into this form (x,y) to avoid mistakes about position!!
	while(!Q.empty())
	{
		state now = Q.front(); Q.pop();
		tmp = now.code;
		for(int i = 0; i < 4; ++ i)
		{
			p[i] = tmp % 8;
			tmp >>= 3;
		}
		x = (now.pos - 1) / 4, y = (now.pos - 1) % 4;
		for(int i = 0; i < 9; ++ i)
		{
			state next = state(now.turn + 1, (x + dx[i]) * 4 + y + 1 + dy[i], 0);
			if(!valid(x + dx[i], y + dy[i])) continue;
			
			switch(next.pos)
			{
				case 1:
				{
					next.code = (p[1] + 1) * 8 + (p[2] + 1) * 64 + (1 + p[3]) * 512;
					break;
				}
				case 3:
				{
					next.code = p[0] + 1 + (p[2] + 1) * 64 + (1 + p[3]) * 512;
					break;
				}
				case 9:
				{
					next.code = p[0] + 1 + (p[1] + 1) * 8 + (1 + p[3]) * 512;
					break;
				}
				case 11:
				{
					next.code = p[0] + 1 + (p[1] + 1) * 8 + (1 + p[2]) * 64;
					break;
				}
				default:
				{
					next.code = p[0] + 1 + (p[1] + 1) * 8 + (p[2] + 1) * 64 + (1 + p[3]) * 512;
					break;
				}
			}
			if(!valid(next)) continue;
			if(next.turn == n)return true;
			vis[next.turn][next.pos][next.code] = true;
			Q.push(next);
		}
	}
	return false;
}
int main()
{
	while(scanf("%d", &n) == 1)
	{
		if(!n) return 0;
		memset(day, false, sizeof(day));
		for(int i = 1; i <= n; ++ i)
			for(int j = 1; j <= 16; ++ j) scanf("%d", &day[i][j]);
		printf("%d\n", bfs());
	}
	return 0;
}/*
challenge : 1. the design of state
   			2. the extention of states
   			3. the pictures of position
*/

Guess you like

Origin www.cnblogs.com/zach20040914/p/12633195.html