USACO 2.4 The Tamworth Two<简单图问题>

meaning:题意是给一张图,图中有牛的位置,农场主的位子和障碍物。牛和农场主只能沿着一个方向走直到要出界或者走到障碍物就转弯!!(注意是走到障碍物才转弯,而不是碰到之前转弯),转弯的方向为顺时针,问农场主是否可以和牛相遇。

the reason of failure:1、需要注意是走到障碍物才转弯。

2、给的图有C和F,如果只遇到'.'才走下去显然是错的,遇到C||F都可以接着走下去。

3、判定不能走的条件不仅是他们在之前走过的位子,还要在这个位子面对的方向与之前相同。

thinking:直接按着题意模拟走就行,认真看题再码,别着急

代码:

/*
ID: me
PROG: ttwo
LANG: C++
*/
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <queue>
using namespace std;
struct ttt{
int r[2];
int c[2];
int d[2];
int step;
int l[2];
};
char map1[12][12];
int walked[11][11][11][11][11][11];
int dd(ttt a,int b){
    if(a.r[b]>=1&&a.r[b]<=10&&a.c[b]>=1&&a.c[b]<=10)return 1;
    else return 0;
}
int dd2(char c){
    if(c=='.'||c=='F'||c=='C')return 1;
    else return 0;
}
int main(){
    freopen("ttwo.in","r",stdin);
    freopen("ttwo.out","w",stdout);
    int i,j,k,l,f1,f2,f3,t71,t2,t3;
    ttt s1,s2;
    memset(map1,0,sizeof(map1));
    memset(walked,0,sizeof(walked));
    for(i=1;i<=10;i++)
    for(j=1;j<=10;j++){
        cin >> map1[i][j];
        if(map1[i][j]=='F'){
            s1.r[0]=i;
            s1.c[0]=j;
        }else if(map1[i][j]=='C'){
            s1.r[1]=i;
            s1.c[1]=j;
        }
    }
    s1.step=0;
    s1.d[0]=0;
    s1.d[1]=0;
    queue<ttt>qq;
    qq.push(s1);
    while(!qq.empty()){
        s1=qq.front();qq.pop();
        //cout << s1.r[0] << "," << s1.c[0] << "-" <<s1.d[0] << "   "
        //<<s1.r[1] << "," << s1.c[1] << "-" << s1.d[1];
        if(s1.r[0]==s1.r[1]&&s1.c[0]==s1.c[1]){
            break;
        }
        //cout << "   没出去 ";
        //if(s1.l[0]==0){
        if(s1.d[0]==0){
            if(dd2(map1[s1.r[0]][s1.c[0]])&&dd2(map1[s1.r[0]-1][s1.c[0]])){
                s1.r[0]--;
            }else{
                s1.d[0]=(s1.d[0]+1)%4;
            }
        }else if(s1.d[0]==3){
            if(dd2(map1[s1.r[0]][s1.c[0]])&&dd2(map1[s1.r[0]][s1.c[0]-1])){
                s1.c[0]--;
            }else{
                s1.d[0]=(s1.d[0]+1)%4;
            }
        }else if(s1.d[0]==2){
        if(dd2(map1[s1.r[0]][s1.c[0]])&&dd2(map1[s1.r[0]+1][s1.c[0]])){
                s1.r[0]++;
            }else{
                s1.d[0]=(s1.d[0]+1)%4;
            }
            }else if(s1.d[0]==1){
            if(dd2(map1[s1.r[0]][s1.c[0]])&&dd2(map1[s1.r[0]][s1.c[0]+1])){
                s1.c[0]++;
            }else{
                s1.d[0]=(s1.d[0]+1)%4;
            }
            }

            //if(s1.l[1]==0){
            if(s1.d[1]==0){
            if(dd2(map1[s1.r[1]][s1.c[1]])&&dd2(map1[s1.r[1]-1][s1.c[1]])){
                s1.r[1]--;
            }else{
                s1.d[1]=(s1.d[1]+1)%4;
            }
        }else if(s1.d[1]==3){
            if(dd2(map1[s1.r[1]][s1.c[1]])&&dd2(map1[s1.r[1]][s1.c[1]-1])){
                s1.c[1]--;
            }else{
                s1.d[1]=(s1.d[1]+1)%4;
            }
        }else if(s1.d[1]==2){
        if(dd2(map1[s1.r[1]][s1.c[1]])&&dd2(map1[s1.r[1]+1][s1.c[1]])){
                s1.r[1]++;
            }else{
                s1.d[1]=(s1.d[1]+1)%4;
            }
            }else if(s1.d[1]==1){
            if(dd2(map1[s1.r[1]][s1.c[1]])&&dd2(map1[s1.r[1]][s1.c[1]+1])){
                s1.c[1]++;
            }else{
                s1.d[1]=(s1.d[1]+1)%4;
            }
            }

        //    cout << s1.r[0] << "," << s1.c[0] << "-" <<s1.d[0] << "   "
      //  <<s1.r[1] << "," << s1.c[1] << "-" << s1.d[1] << "  " ;
    //    cout << walked[s1.r[0]][s1.c[0]][s1.d[0]][s1.r[1]][s1.c[1]][s1.d[1]] << endl;
            if(walked[s1.r[0]][s1.c[0]][s1.d[0]][s1.r[1]][s1.c[1]][s1.d[1]]==0){
                walked[s1.r[0]][s1.c[0]][s1.d[0]][s1.r[1]][s1.c[1]][s1.d[1]]=s1.step;
                s1.step++;
  //              cout << " 接着走" <<s1.step << endl;
                qq.push(s1);
            }
    }
    //cout << endl;

    if(s1.r[0]==s1.r[1]&&s1.c[0]==s1.c[1]){
            cout << s1.step << endl;
        }else{
        cout << 0 <<endl;
        }
    return 0;
}




回答: 题目P1518 \[USACO2.4\] 两只塔姆沃斯牛是一道模拟题,题目要求判断Farmer John和两头牛是否会相遇。解题思路可以分为两种方法。一种方法是记录二者的状态,如果出现了与前面相同的状态则说明陷入了死循环。具体实现步骤可以使用数组来记录Farmer John和两头牛的坐标、方向等状态信息,然后判断是否出现了重复的状态。另一种方法是利用博弈的思想,如果二者会同时回到一种状态,那么说明他们不会再相遇了,因为这时候他们已经陷入了一种对称性的状态。通过判断是否存在一种线性关系,可以确定二者是否会相遇。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two](https://blog.csdn.net/TD123456q/article/details/125688037)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [洛谷P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two 题解 (C/C++)](https://blog.csdn.net/Jason__Jie/article/details/115027619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [(移动方向状态标志)P1518 [USACO2.4]两只塔姆沃斯牛 The Tamworth Two题解](https://blog.csdn.net/m0_57221330/article/details/119980758)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值