【模拟】Luogu P1518 两只塔姆沃思牛(The Tamworth Two)

点击进入FJ的牧场


据说是道BFS,但是好像只有遇到解直接输出跟BFS有点关系,除此之外完全纯模拟

提交时间 2016-11-09 17:12
耗时/内存 52ms , 16410kb (巨慢)

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <queue>

const int maxn = 20;

using namespace std;

int cnt;
int ten1 = 1,ten2 = 1;
int dx[5] = {0,1,0,-1,0};
int dy[5] = {0,0,1,0,-1};
char map[maxn][maxn];
int fx,fy,cx,cy;

struct node
{
    int x,y; 
};

bool check(node ha)
{
    if(ha.x <= 0||ha.x > 10||ha.y <= 0||ha.y > 10||map[ha.x][ha.y] == '*')
    {
        //cout<<"check is 0"<<"\n"<<endl;
        return false;    
    } 
    //cout<<"check is 1"<<"\n"; 
    return true;
}

inline void bfs()
{
    node fj,tt;
    fj.x = fx;
    fj.y = fy;
    tt.x = cx;
    tt.y = cy;
    for(;;)
    {
        cnt++;
        //cout<<"这里是cnt: "<<cnt<<endl;
        if(cnt >= 1000) {printf("0\n");return ;} 
        if(ten1 == 1) { fj.x -= 1;if(check(fj) == false) { ten1 = 2; fj.x += 1;} }
        else if(ten1 == 2) { fj.y += 1; if(check(fj) == false) { ten1 = 3; fj.y -= 1;} }
        else if(ten1 == 3) { fj.x += 1; if(check(fj) == false) { ten1 = 4; fj.x -= 1;} }
        else if(ten1 == 4) { fj.y -= 1; if(check(fj) == false) { ten1 = 1; fj.y += 1;} }
        //cout<<"哔哔这里是农民约翰("<<fj.x<<","<<fj.y<<")收到请回答"<<endl;
        if(ten2 == 1) { tt.x -= 1;if(map[tt.x][tt.y] == '*' || check(tt) == false) {ten2 = 2;tt.x += 1;} }
        else if(ten2 == 2) { tt.y += 1; if(check(tt) == false) { ten2 = 3; tt.y -= 1;} }
        else if(ten2 == 3) { tt.x += 1; if(check(tt) == false) { ten2 = 4; tt.x -= 1;} }
        else if(ten2 == 4) { tt.y -= 1; if(check(tt) == false) { ten2 = 1; tt.y += 1;} }
        //cout<<"哔哔这里是两只塔姆沃思牛("<<tt.x<<","<<tt.y<<")我们已经收到,再见垃圾约翰~"<<endl;
        //cout<<"约翰:“WTF???”"<<endl;
        //以上为模拟行进
        if(fj.x == tt.x&&fj.y == tt.y) {printf("%d\n",cnt);return ;}
    }

}

int main()
{
    for(int i = 1;i <= 10;i++)
        for(int j = 1;j <= 10;j++)
        {
            cin>>map[i][j];
            if(map[i][j] == 'F') {fx = i;fy = j;}
            if(map[i][j] == 'C') {cx = i;cy = j;}    
        }  
        bfs();
return 0;
}

THE END

By Peacefuldoge

http://blog.csdn.net/loi_peacefuldog

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值