测试点3/6/8WA,不能追上的经验教训
查看原帖
测试点3/6/8WA,不能追上的经验教训
160570
sjyh_jy楼主2023/5/24 23:02

刚开始交循环次数开小了,WA。

改成1e10TLE,然后改成了1e8,还是WA。 下载数据后,发现没追上的情况下,n没到0也提前结束循环了,导致输出加了超多次的ans,而不是0。

#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <cctype>
#include <vector>
#include <queue>
#include <algorithm>
#include <set>
#include <map>

using namespace std;


int main(){
    char map[11][11];
    int fi,fj,ci,cj;
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            cin>>map[i][j];
            if(map[i][j]=='F'){
                fi=i;
                fj=j;
            }
            if(map[i][j]=='C'){
                ci=i;
                cj=j;
            }
        }
    }
    int dir[4][2]={{-1,0},{0,1},{1,0},{0,-1}};//上 右 下 左
    int n=1e8;
    int ans=0,k1=0,k2=0;//k代表方向
    while(n--){
        //模拟行动
        //人
        int tmpi=fi+dir[k1][0];
        int tmpj=fj+dir[k1][1];
        if(tmpi>=0&&tmpi<10&&tmpj>=0&&tmpj<10&&map[tmpi][tmpj]!='*'){
            fi=tmpi;
            fj=tmpj;
        }else{
            k1=(k1+1)%4;
        }
        //牛
        tmpi=ci+dir[k2][0];
        tmpj=cj+dir[k2][1];
        if(tmpi>=0&&tmpi<10&&tmpj>=0&&tmpj<10&&map[tmpi][tmpj]!='*'){
            ci=tmpi;
            cj=tmpj;
        }else{
            k2=(k2+1)%4;
        }
        //判断追上没
        ans++;
        if(fi==ci&&fj==cj)
            break;
    }
    if(n==0)
        cout<<0;
    else
        cout<<ans;
    return 0;
}

2023/5/24 23:02
加载中...