1. 程式人生 > >HNUST 1695: 跳格子(簡單模擬)

HNUST 1695: 跳格子(簡單模擬)

每次動作後輸出lxl的當前座標,一共有n行。隨後的第n+1行輸出lxl到達過的格子總數。

顯然是一個模擬題,但是本題有一個問題就是面對的方向,思量了十分鐘,始終沒有想到好的解決辦法,只能拿程式碼長度換時間了。

就是用了圖中的最簡單的辦法,導致程式碼挺長,很多地方都是重複的(⊙o⊙)…(直接複製貼上改了點符號)。。。

在就感覺沒什麼了,對於經過的格子數的話,可能跳來跳去會回頭,顯然需要判重,感覺用set是最簡單的,不過鑑於我結構體已經建立了,所以我用的是結構體加sort解決的

貼程式碼

還有額,學弟學妹們,能不能留下點評論啥的,別粘了程式碼就跑吧(⊙o⊙)…

#include <iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<string>
#include<cstdlib>
using namespace std;
typedef long long LL;
struct Node
{
    int xx,yy;
} st[105];
bool in(int x,int y)
{
    if(x>=1&&x<=10&&y>=1&&y<=10)
        return true;
    return false;
}
bool cmp(Node x,Node y)
{
    if(x.xx>y.xx)
        return 0;
    if(x.xx<y.xx)
        return 1;
    return x.yy<y.yy;
}
int main()
{
    int n,x,y,pos,t,xu=0;
    while(~scanf("%d",&n))
    {
        st[xu].xx=1;
        st[xu].yy=1;
        xu++;
 
        x=y=1;
        pos=1; ///post儲存當前方向
        while(n--)
        {
            scanf("%d",&t);
            if(t==1)
            {
                if(pos==1)
                    pos=4;
                else
                    pos--;
            }
            else if(t==2)
            {
                if(pos==4)
                    pos=1;
                else
                    pos++;
            }
            else if(t==3)
            {
                if(pos==1) pos=3;
                else if(pos==3) pos=1;
                else if(pos==4) pos=2;
                else            pos=4;
            }
            else if(t==4)
            {
                if(pos==1)
                {
                    if(in(x-1,y))
                        x--;
                }
                else if(pos==2)
                {
                    if(in(x,y+1))
                        y++;
                }
                else if(pos==3)
                {
                    if(in(x+1,y))
                        x++;
                }
                else
                {
                    if(in(x,y-1))
                        y--;
                }
                st[xu].xx=x;
                st[xu].yy=y;
                xu++;
            }
            else if(t==6)
            {
                if(pos==1)
                {
                    if(in(x+1,y))
                        x++;
                }
                else if(pos==2)
                {
                    if(in(x,y-1))
                        y--;
                }
                else if(pos==3)
                {
                    if(in(x-1,y))
                        x--;
                }
                else
                {
                    if(in(x,y+1))
                        y++;
                }
                st[xu].xx=x;
                st[xu].yy=y;
                xu++;
            }
            else if(t==5)
            {
                if(pos==1)
                {
                    if(in(x,y+1))
                        y++;
                }
                else if(pos==2)
                {
                    if(in(x+1,y))
                        x++;
                }
                else if(pos==3)
                {
                    if(in(x,y-1))
                        y--;
                }
                else
                {
                    if(in(x-1,y))
                        x--;
                }
                st[xu].xx=x;
                st[xu].yy=y;
                xu++;
            }
            else
            {
                if(pos==1)
                {
                    if(in(x,y-1))
                        y--;
                }
                else if(pos==2)
                {
                    if(in(x-1,y))
                        x--;
                }
                else if(pos==3)
                {
                    if(in(x,y+1))
                        y++;
                }
                else
                {
                    if(in(x+1,y))
                        x++;
                }
                st[xu].xx=x;
                st[xu].yy=y;
                xu++;
 
            }
            printf("(%d,%d)\n",x,y);
        }
        sort(st,st+xu,cmp);
        int sum=1;
        for(int i=0;i<xu-1;i++)
        {
            if(st[i].xx!=st[i+1].xx||st[i].yy!=st[i+1].yy)
                sum++;
            //printf("%d   %d \n",st[i].xx,st[i].yy);
        }
        printf("%d\n",sum);
    }
    return 0;
}
 
/**************************************************************
    Problem: 1695
    User: 水題小王子
    Language: C++
    Result: 正確
    Time:0 ms
    Memory:1704 kb
****************************************************************/