HDU 1010 Tempter of the Bone

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   io   for   art   

第一次剪枝,只不過不是我自己剪的,學習別人的代碼。

我感覺ACM一開始就是學習學習在學習,等弄懂一定量的題目以後,才會慢慢有自己的思路,自己的風格。

題意:有一個迷宮,給出一個起點和終點,問能否正好在第t步走到終點,每個方格只能走一遍。

兩次剪枝都在代碼注釋裡面。

 

這是搜尋的第一篇,加油(^ω^)

 1 //#define LOCAL 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7  8 char map[10][10]; 9 int dir[4][2] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};10 int flag, Sx, Sy, Dx, Dy, Xnum;11 int n, m, t;12 13 void DFS(int x, int y, int time)14 {15     if(x<1 || x>n || y<1 || y>m)16         return;17     if(flag == 1)18         return;19     if(x == Dx && y == Dy && time == t)20     {21         flag = 1;22         return;23     }24 25     int temp = (t-time) - abs(x-Dx) - abs(y-Dy);26     if(temp<0 || temp&1)    //剩餘時間不足或者奇偶性不同,剪枝27         return;28     for(int i = 0; i < 4; ++i)29     {30         int x1 = x + dir[i][0];31         int y1 = y + dir[i][1];32         if(map[x1][y1] != ‘X‘)33         {34             map[x1][y1] = ‘X‘;35             DFS(x1, y1, time + 1);36             map[x1][y1] = ‘.‘;37         }38     }39     return;40 }41 42 int main(void)43 {44     #ifdef LOCAL45         freopen("1010in.txt", "r", stdin);46     #endif47 48     while(cin >> n >> m >> t)49     {50         if(n==0 && m==0 && t==0)51             break;52         Xnum = 0;53         for(int i = 1; i <= n; ++i)54             for(int j = 1; j <= m; ++j)55             {56                 cin >> map[i][j];57                 if(map[i][j] == ‘S‘)58                 {59                     Sx = i;60                     Sy = j;61                 }62                 if(map[i][j] == ‘D‘)63                 {64                     Dx = i;65                     Dy = j;66                 }67                 if(map[i][j] == ‘X‘)68                     ++Xnum;69             }70         flag = 0;71         map[Sx][Sy] = ‘X‘;72 73         if(n*m - Xnum <= t)74         {    //所給t過大,剪枝75             printf("NO\n");76             continue;77         }78         DFS(Sx, Sy, 0);79         if(flag)80             printf("YES\n");81         else82             printf("NO\n");83     }84     return 0;85 }
代碼君

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.