poj2044 Weather Forecast 搜索

42 篇文章 0 订阅

vis判重 几个值分别表示 日期 左上角所在位置(只会在9个地方出现) 人间四个角干旱的天数。

因为day用位运算记录的状态,第一个点在最开头,要注意。

#include <cstring>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <map>
#include <queue>
using namespace std;
int vis[370][10][8][8][8][8];
int day[370],n;
int dx[10]={0,0,1,2,-1,-2,0,0,0,0};
int dy[10]={0,0,0,0,0,0,1,2,-1,-2};
struct node
{
    int d1,d2,d3,d4;
};
int get(int x,int y)
{
    int num=(x-1)*4+y;
    return (1<<(16-num));
}
bool check(int date,int x,int y,node corner)
{
    if(corner.d1==7||corner.d2==7||corner.d3==7||corner.d4==7)
    {
        return false;
    }
    int flag=0;
    flag|=get(x,y);
    flag|=get(x,y+1);
    flag|=get(x+1,y);
    flag|=get(x+1,y+1);
    if(flag&day[date])
    {
        return false;
    }
    if(vis[date][(x-1)*3+y][corner.d1][corner.d2][corner.d3][corner.d4])
    {
        return false;
    }
    vis[date][(x-1)*3+y][corner.d1][corner.d2][corner.d3][corner.d4]=true;
    return true;
}
bool dfs(int date,int x,int y,node corner)
{
    if(date==n+1)
    {
        return true;
    }
    if(!check(date,x,y,corner))
    {
        return false;
    }
    int xx,yy;
    node tmp;
    for(int i=1;i<=9;i++)
    {
        tmp=corner;
        tmp.d1++;
        tmp.d2++;
        tmp.d3++;
        tmp.d4++;
        xx=x+dx[i];
        yy=y+dy[i];
        if(xx>=1&&xx<=3&&yy>=1&&yy<=3)
        {
            if(xx==1&&yy==1)
            {
                tmp.d1=0;
            }
            if(xx==1&&yy==3)
            {
                tmp.d2=0;
            }
            if(xx==3&&yy==1)
            {
                tmp.d3=0;
            }
            if(xx==3&&yy==3)
            {
                tmp.d4=0;
            }
            if(dfs(date+1,xx,yy,tmp))
            {
                return true;
            }
        }
    }
    return false;
}
int main()
{
    while(scanf("%d",&n))
    {
        if(n==0)
        {
            break;
        }
        int tmp;
        for(int i=1;i<=n;i++)
        {
            day[i]=0;
            for(int j=1;j<=16;j++)
            {
                scanf("%d",&tmp);
                day[i]<<=1;
                day[i]|=tmp;
            }
        }
        memset(vis,0,sizeof(vis));
        node start;
        start.d1=1;
        start.d2=1;
        start.d3=1;
        start.d4=1;
        if(dfs(1,2,2,start))
        {
            printf("1\n");
        }
        else
        {
            printf("0\n");
        }
    }
    return 0;
}
/*
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
*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值