【codeforces 793B】Igor and his way to work

【题目链接】:http://codeforces.com/contest/793/problem/B

【题意】

给一个n*m大小的方格;
有一些方格可以走,一些不能走;
然后问你从起点到终点,能不能在转弯次数不超过2的情况下达到;

【题解】

记忆化搜索写一个;
f[x][y][z]表示,到了x,y这个坐标,当前方向是z的最小转弯次数;
按照这个数组;
写一个dfs就好;
不会难.

【Number Of WA

2(一开始没加第三维TAT)

【完整代码】

#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define ms(x,y) memset(x,y,sizeof x)

typedef pair<int,int> pii;
typedef pair<LL,LL> pll;

const int dx[9] = {0,1,0,-1,0,-1,-1,1,1};
const int dy[9] = {0,0,-1,0,1,-1,1,-1,1};
const double pi = acos(-1.0);
const int N = 1e3+100;

int n,m,a[N][N],x0,y0,x1,y1,f[N][N][5];
char s[N];

void dfs(int x,int y,int pre,int num)
{
    if (a[x][y]==0) return;
    if (num>2) return;
    if (f[x][y][pre]!=-1 && f[x][y][pre]<=num) return;

    f[x][y][pre] = num;
    rep1(i,1,4)
    {
        int tx = x+dx[i],ty = y + dy[i];
        int before = pre+2;
        if (before>4) before-=4;
        if (pre!=0 && i==before) continue;
        if (pre==0)
            dfs(tx,ty,i,num);
        else
        {
            if (pre!=i)
                dfs(tx,ty,i,num+1);
            else
                dfs(tx,ty,i,num);
        }
    }
}

int main()
{
    //freopen("F:\\rush.txt","r",stdin);
    ios::sync_with_stdio(false),cin.tie(0);//scanf,puts,printf not use
    cin >> n >> m;
    rep1(i,1,n)
    {
        cin>>(s+1);
        rep1(j,1,m)
            if (s[j]=='*')
                a[i][j] = 0;
            else
            {
                a[i][j] = 1;
                if (s[j]=='S')
                    x0= i,y0=j;
                if (s[j]=='T')
                    x1 =i,y1 = j;
            }
    }
    ms(f,255);
    dfs(x0,y0,0,0);
    rep1(i,1,4)
        if (f[x1][y1][i]!=-1)
            return cout <<"YES"<<endl,0;
    cout <<"NO"<<endl;
    return 0;
}
posted @ 2017-10-04 18:44  AWCXV  阅读(132)  评论(0编辑  收藏  举报