您的位置:首页 > 其它

USACO--2.4The Tamworth Two

2015-03-26 16:29 239 查看
这题目一看就是一个模拟题,但是问题在于我们什么时候结束这次的模拟判断他们不可能相遇。开始的时候我想了几种可能循环的情况作为结束的判断条件,但都是超时;然后我想可不可设置一个时间上限来作为结束模拟的条件,然后我就设了一个上限100000,没想到AC了,后面我给改成1000还是可以AC。。。。。。。

后面看了一下官方的解答,我的思路是对的,但是上限设小了。考虑将一个格子的坐标加上站在这个格子中时面向的方向作为一个状态,那么每个人有4*10*10=400个状态,然后两个人状态的组合数为400*400=160000,如果我们模拟160000次走遍这些状态然后还是不能相遇那么就肯定不能相遇了。所以正确的上限是160000

代码如下:

/*
ID:15674811
LANG:C++
PROG:ttwo
*/

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<fstream>
using namespace std;

#define maxn 12

ofstream fout("ttwo.out");
ifstream fin("ttwo.in");

       ///北西南东
int dx[]={-1,0,1,0};
int dy[]={0,1,0,-1};
char M[maxn][maxn];

typedef struct
{
    int x,y;
    int d;  ///方向
}P;
P stf,stc,stf1,stc1;

void run(P &st)
{
    int x=st.x,y=st.y,d=st.d;
    int xx=x,yy=y;
    xx=x+dx[d];
    yy=y+dy[d];
    if(xx<1||xx>10||yy<1||yy>10)
    {
        d=(d+1)%4;
        st.d=d;
        return ;
    }
    if(M[xx][yy]=='*')
    {
         d=(d+1)%4;
         st.d=d;
         return ;
    }
    st.x=xx; st.y=yy; st.d=d;
}

void solve()
{
    stf.x=stf1.x; stf.y=stf1.y; stf.d=stf1.d;
    stc.x=stc1.x; stc.y=stc1.y; stc.d=stc1.d;
    int t=0;
    while(true)
    {
        t++;
        run(stf);
        run(stc);
        if(stf.x==stc.x&&stf.y==stc.y)
            break;
        if(t>160000)
        {
            t=0;
            break;
        }
    }
    fout<<t<<endl;
}
int main()
{
    //ifstream fin("lkl.txt");
    for(int i=1;i<=10;i++)
       for(int j=1;j<=10;j++)
       {
            fin>>M[i][j];
            if(M[i][j]=='F')
            {
                stf1.x=i;
                stf1.y=j;
                stf1.d=0;
            }
            if(M[i][j]=='C')
            {
                stc1.x=i;
                stc1.y=j;
                stc1.d=0;
            }
       }
     solve();
  return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: