2014 ACM/ICPC Asia Regional Guangzhou Online HDU 5024 Wang Xifeng's Little Plot

就是简单的搜索题====

渣渣搞了4个小时

暴力寻找, 250MS==

代码

#include <stdio.h>
#include <cstring>
#include <queue>
using namespace std;
#define N 105
struct Point
{
    int fx;
    int x, y;
    int step;
    int num;
    int px, py;
}point;
int move[9][2] = {0,0, 0,-1, 0,1, -1,0, 1,0, -1,-1, -1,1, 1,-1, 1,1};
char map[N][N];
int vis[N][N][9];
int n, max1;
queue<Point>q;

void bfs();
int main()
{
    while(scanf("%d", &n) ,n)
    {
        getchar();
        int i, j;
        for(i=0;i<n;i++)
        {
            gets(map[i]);
        }
        memset(vis, 0, sizeof(vis));
        max1 = 0;
        for(i=0; i<n; i++)
        {
            for(j=0; j<n; j++)
            {
                if(map[i][j] == '.')
                {
                    point.x = i;
                    point.y = j;
                    point.fx = 0;
                    point.step = 1;
                    point.num = -1;
                    point.px = 0;
                    point.py = 0;
                    bfs();
                }
            }
        }
        printf("%d\n", max1);
    }
    return 0;
}

void bfs()
{
    while(!q.empty()) q.pop();
    q.push(point);

    while(!q.empty())
    {
        Point now = q.front();
        q.pop();
        if(now.step > max1)
        {
            max1 = now.step;
        }
        //printf("i=%d, j=%d, num=%d, fx=%d, step=%d, px=%d, py=%d\n", now.x, now.y, now.num, now.fx, now.step, now.px, now.py);
        for(int i=1; i<=8; i++)
        {
            int xx = now.x, yy = now.y;
            Point next = now;
            next.px = now.x;
            next.py = now.y;

            xx += move[i][0];
            yy += move[i][1];
            if(map[xx][yy] == '#') continue;
            if(xx<0 || xx>=n || yy<0 || yy>=n) continue;
            //if(xx==point.x && yy==point.y) continue;
            //printf("%d %d\n", xx, yy);
            if(now.fx>4 && now.fx<=8 && i<=4) continue;
            if(now.fx<=4 && now.fx>0 && i>4) continue;
            if(now.fx != i) next.num++;
            if(next.num > 1) continue;

            next.x = xx;
            next.y = yy;
            next.step++;
            next.fx = i;

            if(next.x==now.px && next.y==now.py) continue;//printf("%d %d %d %d\n", next.x, next.y, next.px, next.py);
            if(!vis[xx][yy][i])
            {
                vis[xx][yy][i] = next.step;
                q.push(next);
                continue;
            }
            if(vis[xx][yy][i] < next.step)
            {
                vis[xx][yy][i] = next.step;
                q.push(next);
                continue;
            }
            /*
            if(now.fx>4 && now.fx<=8 && vis[xx][yy]<=4)
            {
                vis[xx][yy] = i;
                q.push(next);
                continue;
            }
            if(now.fx<=4 && now.fx>0 && vis[xx][yy]>4)
            {
                vis[xx][yy] = i;
                q.push(next);
                continue;
            }
            */
        }
    }
}
/*
6
.###.#
#.#.##
##.###
#.####
##.###
###.##
*/



 

转载于:https://www.cnblogs.com/tenlee/p/4420123.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值