首页 > ACM题库 > HDU-杭电 > HDU 3137-No Left Turns[解题报告]HOJ
2014
03-03

HDU 3137-No Left Turns[解题报告]HOJ

No Left Turns

问题描述 :


Taunt Generation Simulator

ALL HEADS: You’re a Knight of the Round Table?

ROBIN: I am.

LEFT HEAD: In that case I shall have to kill you.

MIDDLE HEAD: Shall I?

RIGHT HEAD: Oh, I don’t think so.

MIDDLE HEAD: Well, what do I think?

LEFT HEAD: I think kill him.

RIGHT HEAD: Well let’s be nice to him.

MIDDLE HEAD: Oh shut up.

As the story goes, the Knight scarpers off. Right Head has taken it upon himself to search the grounds for the knight so he, Left, and Middle can go extinguish him (and then have tea and biscuits.)

Consider the following 8 by 12 maze, where shaded squares are walls that can’t be entered.

Taunt Generation Simulator

The shortest path between the Right Head (denoted by the S, for start) and the knight (denoted by the F, for finish) is of length 3, as illustrated above. But! Right Head can’t turn left or make UTurns. He can only move forward and turn right. That means the shortest path that Right Head can find is significantly longer: at 29!

Taunt Generation Simulator

输入:

The input file will consist of a single integer N (N > 0) specifying the number of mazes in the file. Following this, on a maze by maze basis will be the number of rows, r (3 < r <= 20), a space, then the number of columns, c (3 < c <= 20). After this will follow r lines of c characters, representing a map of the maze:

XXXXXXXXXXXXXX
X XXX
X XFXXXXX X
XXX XX XX X
X S X
XX XXXXXX X X
X X X X
X X X X X
XXX XX X
XXXXXXXXXXXXXX

X’s mark those locations that are walls and can’t be occupied. S marks the start location, and F marks the Knight. Blanks are locations that can be freely traveled.

输出:

The input file will consist of a single integer N (N > 0) specifying the number of mazes in the file. Following this, on a maze by maze basis will be the number of rows, r (3 < r <= 20), a space, then the number of columns, c (3 < c <= 20). After this will follow r lines of c characters, representing a map of the maze:

XXXXXXXXXXXXXX
X XXX
X XFXXXXX X
XXX XX XX X
X S X
XX XXXXXX X X
X X X X
X X X X X
XXX XX X
XXXXXXXXXXXXXX

X’s mark those locations that are walls and can’t be occupied. S marks the start location, and F marks the Knight. Blanks are locations that can be freely traveled.

样例输入:

1
10 14
XXXXXXXXXXXXXX
X          XXX
X XFXXXXX    X
XXX   XX  XX X
X S          X
XX  XXXXXX X X
X        X X X
X X      X X X
XXX XX       X
XXXXXXXXXXXXXX

样例输出:

29

#pragma comment(linker,"/STACK:102400000,102400000")
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <functional>
#include <numeric>
#include <deque>
#include <list>
#include <map>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <iterator>
#include <memory>
#include <utility>
#include <string>

using namespace std;

#define X first
#define Y second
#define mp make_pair
#define pb push_back
#define mset(a) memset(a,0,sizeof(a))
#define mmset(a) memset(a,-1,sizeof(a))
#define mcpy(a,b) memcpy(a,b,sizeof(a))
#define lowbit(a) ((a)&(-(a)))

typedef double lf;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<lf,lf> pdd;
typedef pair<int,pii> pip;
typedef pair<pii,int> ppi;
typedef vector<int> vi;
typedef vector<pii> vpii;

const int inf=1000000007;
const ll linf=1LL<<62;
const ull ulinf=1ULL<<63;
const lf eps=0.000001;
const lf pi=3.14159265358979323846;

template <class T> T abs(T a){return a>=0?a:-a;}
template <class T> T sqr(T a){return a*a;}
template <class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template <class T> T mod(T a,T b){return (a%b+b)%b;}
ll powmod(ll a,ll b,ll c){if(b==0LL)return 1LL;ll ret=powmod(a,b>>1,c);ret=ret*ret%c;if(b&1LL)ret=ret*a%c;return ret;}
ll inv(ll a,ll b){return powmod(a,b-2LL,b);}
template <class T> void maxe(T &a,T b){if(a<b)a=b;}
template <class T> void mine(T &a,T b){if(a>b)a=b;}
int iszero(lf a){return abs(a)<=eps;}

template <class T> void geti(T &a){a=0;T b=1;char c=getchar();if(c=='-')b=-1;else a=c-48;while((c=getchar())!=' '&&c!='\n')a=a*10+c-48;a*=b;}

void fileio_in_out(){freopen(".in","r",stdin);freopen(".out","w",stdout);}
void fileio_txt(){freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);}

//==================================================

#define DEBUG(X) 

const int N=22;
const int M=1111111;
const int K=9;

int test;
int n,m,k,ans;
char s[N][N];
int d[N][N][4];
pii st,ed;
ppi q[N*N*N*N];
int head,tail,inq[N][N][4];
const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};

void push(int x,int y,int z)
{
    if(inq[x][y][z])
        return;
    inq[x][y][z]=1;
    q[++head]=mp(mp(x,y),z);
}

int check(int x,int y)
{
    return x>=1&&x<=n&&y>=1&&y<=m;
}

int main()
{
    //fileio_in_out();
    //fileio_txt();
    //ios::sync_with_stdio(false);
    
    scanf("%d",&test);
    while(test--)
    {
        scanf("%d%d\n",&n,&m);
        for(int i=1;i<=n;i++)
            gets(s[i]+1);
        
        for(int i=1;i<=n;i++)
            for(int j=1;j<=m;j++)
            {
                for(int k=0;k<4;k++)
                    d[i][j][k]=inf;
                if(s[i][j]=='S')
                    st=mp(i,j);
                if(s[i][j]=='F')
                    ed=mp(i,j);
            }
        
        head=tail=0;
        for(int k=0;k<4;k++)
        {
            d[st.X][st.Y][k]=0;
            push(st.X,st.Y,k);
        }
        while(head>tail)
        {
            ppi t=q[++tail];
            int x=t.X.X,y=t.X.Y,z=t.Y;
            if(check(x+dx[z],y+dy[z])&&s[x+dx[z]][y+dy[z]]!='X'&&d[x+dx[z]][y+dy[z]][z]>d[x][y][z]+1)
            {
                d[x+dx[z]][y+dy[z]][z]=d[x][y][z]+1;
                push(x+dx[z],y+dy[z],z);
            }
            int zz=(z+1)%4;
            if(check(x+dx[zz],y+dy[zz])&&s[x+dx[zz]][y+dy[zz]]!='X'&&d[x+dx[zz]][y+dy[zz]][zz]>d[x][y][z]+1)
            {
                d[x+dx[zz]][y+dy[zz]][zz]=d[x][y][z]+1;
                push(x+dx[zz],y+dy[zz],zz);
            }
            inq[x][y][z]=0;
        }
        //printf("%d %d %d %d\n",st.X,st.Y,ed.X,ed.Y);
        ans=inf;
        for(int k=0;k<4;k++)
            mine(ans,d[ed.X][ed.Y][k]);
        printf("%d\n",ans);
    }
    
    //system("pause");
    return 0;
}

  1. 这道题这里的解法最坏情况似乎应该是指数的。回溯的时候
    O(n) = O(n-1) + O(n-2) + ….
    O(n-1) = O(n-2) + O(n-3)+ …
    O(n) – O(n-1) = O(n-1)
    O(n) = 2O(n-1)

  2. int half(int *array,int len,int key)
    {
    int l=0,r=len;
    while(l<r)
    {
    int m=(l+r)>>1;
    if(key>array )l=m+1;
    else if(key<array )r=m;
    else return m;
    }
    return -1;
    }
    这种就能避免一些Bug
    l,m,r
    左边是l,m;右边就是m+1,r;