持续求助只输出0!(按照大佬说的改了还是不行)含详细的代码注释
查看原帖
持续求助只输出0!(按照大佬说的改了还是不行)含详细的代码注释
601747
xibaohe楼主2023/1/6 21:25
#include<bits/stdc++.h>
using namespace std;
int fx,fy,cx,cy;//fx和fy表示FJ的位置,cx和cy表示Cow的位置 
char ft,ct;//ft表示FJ的方向,ct表示Cow的方向 
char mp[15][15];//mp数组存地图 
void gomovef(int a,int b,char c)
{
    int na=a,nb=b;//假设走路后的a和b 
    if(c=='N')//走路 
    nb--;
    if(c=='E')//走路 
    na++;
    if(c=='S')//走路 
    nb++;
    if(c=='W')//走路 
    na--;
    if(mp[na][nb]!='*')//判断是否遇到障碍物 
    {
        fx=na;//转换坐标 
        fy=nb;//转换坐标 
        return;
    }
    if(c=='N')//走不了路,转换方向 
    {
        ft='E';
        return;
    }
    if(c=='E')//走不了路,转换方向 
    {
        ft='S';
        return;
    }
    if(c=='S')//走不了路,转换方向 
    {
        ft='W';
        return;
    }
    if(c=='W')//走不了路,转换方向 
    {
        ft='N';
        return;
    }
}
void gomovec(int a,int b,char c)
{
    int na=a,nb=b;//假设走路后的a和b 
    if(c=='N')//走路 
    nb--;
    if(c=='E')//走路 
    na++;
    if(c=='S')//走路 
    nb++;
    if(c=='W')//走路 
    na--;
    if(mp[na][nb]!='*')//判断是否遇到障碍物 
    {
        cx=na;//转换坐标 
        cy=nb;//转换坐标 
        return;
    }
    if(c=='N')//走不了路,转换方向 
    {
        ct='E';
        return;
    }
    if(c=='E')//走不了路,转换方向 
    {
        ct='S';
        return;
    }
    if(c=='S')//走不了路,转换方向 
    {
        ct='W';
        return;
    }
    if(c=='W')//走不了路,转换方向 
    {
        ct='N';
        return;
    }
}
int main(){
    for(int i=0;i<=11;i++)//把边界赋值成障碍物 
    mp[i][0]='*';
    for(int i=0;i<=11;i++)//把边界赋值成障碍物 
    mp[0][i]='*';
    for(int i=0;i<=11;i++)//把边界赋值成障碍物 
    mp[i][11]='*';
    for(int i=0;i<=11;i++)//把边界赋值成障碍物 
    mp[11][i]='*';
    for(int i=1;i<=10;i++)//循环输入 
    {
        for(int j=1;j<=10;j++)//循环输入 
        {
            cin>>mp[i][j];
            if(mp[i][j]=='F')//判断FJ的位置 
            {
                fx=i;
                fy=j;
                ft='N';
                mp[i][j]='.';
            }
            if(mp[i][j]=='C')//判断Cow的位置 
            {
                cx=i;
                cy=j;
                ct='N';
                mp[i][j]='.';
            }
        }
    }
    for(int i=1;i<=1e6;i++)//循环走路 
    {
        gomovef(fx,fy,ft);//调用函数 
        gomovec(cx,cy,ct);//调用函数 
        if(fx==cx&&fy==cy)//判断是否相遇 
        {
            cout<<i;
            return 0;
        }
    }
    cout<<0;//找不到答案输出0 
    return 0;
}
2023/1/6 21:25
加载中...