百度之星程式設計競賽資格賽:1005-下棋,設計競賽1005-

來源:互聯網
上載者:User

百度之星程式設計競賽資格賽:1005-下棋,設計競賽1005-

</pre><pre name="code" class="cpp">
//思路很簡單:通過廣搜記錄國王到達某個點的最短時間,通過雙for迴圈記錄勇士到達某個點的最短時間;然後找其中較大的那個(不過如果國王先到達的時候需要考慮特殊情況),此題情況要考慮周全。
#include <cstdio>#include <queue>#include <cstring>using namespace std;int n,m,k;int xk,yk,xt,yt;int ans;bool f[1010][1010];<span style="font-family: Arial, Helvetica, sans-serif;">///數組大小分配問題,我也是腦殘了</span>const int ary[8][2]= {{-1,-2},{-2,-1},{-2,1},{-1,2},{1,-2},{2,-1},{2,1},{1,2}};int tans[1010][1010];///其實這個可以不需要,在廣搜的時候可以直接判斷int kans[1010][1010];typedef struct node{    int x,y;    int step;} Node;queue<Node> q;void init_tans(int x,int y){    while(!q.empty())        q.pop();    Node tmp;    tmp.x=x;    tmp.y=y;    tmp.step=0;    q.push(tmp);    f[tmp.x][tmp.y]=true;///對國王初始點進行標記,已經訪問過    while(!q.empty())    {        Node* cnode=&q.front();        q.pop();        for(int i=0; i<8; i++)        {            Node tmp;            tmp.x=cnode->x+ary[i][0];            tmp.y=cnode->y+ary[i][1];            tmp.step=cnode->step+1;            if(tmp.x>=1&&tmp.x<=n&&tmp.y>=1&&tmp.y<=m&&!f[tmp.x][tmp.y])            {                f[tmp.x][tmp.y]=true;                tans[tmp.x][tmp.y]=tmp.step;                q.push(tmp);///竟然忘記入隊列了            }        }    }}void init_kans(){    for(int i=1; i<=n; i++)        for(int j=1; j<=m; j++)        {            int tm;            if(i<xk)                tm=xk-i;            else                tm=i-xk;            if(j<yk)            {                if(tm<yk-j)                    tm=yk-j;            }            else            {                if(tm<j-yk)                    tm=j-yk;            }            kans[i][j]=tm;        }}int main(){    int T;    scanf("%d",&T);    for(int t=1; t<=T; t++)    {        scanf("%d%d%d",&n,&m,&k);        scanf("%d%d",&xk,&yk);        scanf("%d%d",&xt,&yt);        memset(f,0,sizeof(f));        memset(tans,0,sizeof(tans));        memset(kans,0,sizeof(kans));        init_tans(xt,yt);        init_kans();        tans[xt][yt]=2;        kans[xk][yk]=2;        int ans=201;        for(int i=1; i<=n; i++)            for(int j=1; j<=m; j++)            {                if(kans[i][j]!=0&&tans[i][j]!=0)                {                    /// int tmp=max(kans[i][j],tans[i][j]);此處出現問題                    int tmp;<span style="font-family: Arial, Helvetica, sans-serif;">//勇士到達某一點的時間可以是>=最短時間的任何值</span>
                    if(kans[i][j]>tans[i][j])                        tmp=kans[i][j];                    else                    {<span style="font-family: Arial, Helvetica, sans-serif;">//對國王應進行特殊處理,國王到達某一個點的時間是最短時間+2的倍數(特殊情況,如果國王提前到達)</span>
                        if((tans[i][j]-kans[i][j])%2==0)                            tmp=tans[i][j];                        else                            tmp=tans[i][j]+1;                    }                    if(ans>tmp)                        ans=tmp;                }            }        printf("Case #%d:\n",t);        if(ans<=k)            printf("%d\n",ans);        else            printf("OH,NO!\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.