现在的位置: 首页 > 综合 > 正文

POJ 2044 Weather Forecast

2013年11月16日 ⁄ 综合 ⁄ 共 2458字 ⁄ 字号 评论关闭

/*
深度优先搜索,需要记忆状态,状态的记忆包括四个角到目前为止没有下雨的时间
+云当前所在的位置+当前的天数,一开始写错了一个地方害我调了N久,最近为啥
没状态
*/
#include <iostream>
#define MAX_N 370
#define MAX_S 80000
using namespace std;

struct day
{
    int state;
}days[MAX_N + 1];

bool v[MAX_S + 1][MAX_N];

int expv[16], dayn;
int dir[9][2] = {
{0, 0},
{-1, 0}, {-2, 0},
{1,  0}, {2,  0},
{0, -1}, {0, -2},
{0,  1}, {0,  2}};

bool inRange(int x, int y)
{
    return x >= 0 && x <= 2 && y >= 0 && y <= 2;
}

int getRainRange(int curx, int cury)
{
    int range = 0;
    int d1 = curx * 4 + cury;
    int d2 = d1 + 1;
    int d3 = d1 + 4;
    int d4 = d1 + 5;

    range |= expv[d1];
    range |= expv[d2];
    range |= expv[d3];
    range |= expv[d4];

    return range;
}

bool checkNotRainDay(int curstate, int newx, int newy, int &newstate)
{    
    int cstate = curstate;
    int day1 = cstate / 10000;
    cstate = cstate - day1 * 10000;
    int day2 = cstate / 1000;
    cstate = cstate - day2 * 1000;
    int day3 = cstate / 100;
    cstate = cstate - day3 * 100;
    int day4 = cstate / 10;
   
    if(newx != 0 || newy != 0) day1++;
    else day1 = 0;
    if(day1 >= 7) return false;

    if(newx != 0 || newy != 2) day2++;
    else day2 = 0;
    if(day2 >= 7) return false;

    if(newx != 2 || newy != 0) day3++;
    else day3 = 0;
    if(day3 >= 7) return false;

    if(newx != 2 || newy != 2) day4++;
    else day4 = 0;
    if(day4 >= 7) return false;

    newstate = day1 * 10000 + day2 * 1000 + day3 * 100 + day4 * 10;
    int pos = 0;

    if(newx == 0) pos = newy;
    else if(newx == 1) pos = newx * 4 + newy - 1;
    else pos = newx * 4 + newy - 2;

    newstate += pos;

    return true;
}

bool dfs(int curday, int curx, int cury, int curstate)
{
    if(curday == dayn) return true;
    int newx, newy, rainrange, newstate;
   
    for(int d = 0; d < 9; d++)
    {
        if(curday == 0)
        {
            newx = 1;
            newy = 1;
        }
        else
        {
            newx = curx + dir[d][0];
            newy = cury + dir[d][1];
        }       
        if(inRange(newx, newy))
        {
            rainrange = getRainRange(newx, newy);
            if((rainrange & days[curday].state) == 0 &&
                checkNotRainDay(curstate, newx, newy, newstate) && !v[newstate][curday])
            {
                v[newstate][curday] = true;
                if(dfs(curday + 1, newx, newy, newstate))
                    return true;
            }
        }

        if(curday == 0) return false;
    }
    return false;
}

int main()
{
    int i, j;
    expv[0] = 1;
    for(i = 1; i < 16; i++)
        expv[i] = expv[i - 1] * 2;
    while(scanf("%d", &dayn) && dayn != 0)
    {
        int daystate, temp;
        for(i = 0; i < dayn; i++)
        {
            daystate = 0;
            for(j = 0; j < 16; j++)
            {
                scanf("%d", &temp);
                if(temp) daystate = daystate | expv[j];
            }
            days[i].state = daystate;
        }
        memset(v, 0, sizeof(v));
       
        int curstate = 0;
        if(dfs(0, 0, 0, curstate)) printf("1/n");
        else printf("0/n");
    }
    return 0;
}

抱歉!评论已关闭.