HDOJ-2258 Continuous Same Game (1) dfs

來源:互聯網
上載者:User
題目連結:here題意:

        坑人啊!!我被英文給坑人。。寫了兩天!!才看出是題意理解錯了!!弄明白之後果斷AC。。。

        題意是這樣的,就是這種遊戲。。大家都玩過。一個n*m的方格,1-5代表5種顏色的小格子。碰到2個,或者2個以上在一起的小格子就自動消掉。。這個人比較笨,只會一個一個的消掉,先消掉數量最大的,當數量相同的時候,取i小的那個,i 相同就取  j 小的那個,當下面的沒有了,就下移。。一整豎排沒有的時候,左邊的右移(我一開始以為左移!!)。。。然後每次消掉 n 個連續的,得分就是  n*(n+1)。。。算最後的了多少分。

分析:

        先 dfs 那些一樣格子的數量,然後消掉。。最後判斷需不需要下移或者右移。。然後搞定。

代碼:

#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int maxn = 22;int dir[4][2] = {{1,0},{-1,0},{0,1},{0,-1}};int n, m;char map[maxn][maxn];int vis[maxn][maxn];//標記每次是否訪問,並且訪問的是第幾個塊int maxnum[maxn*maxn];//記錄每個標記的塊數int len;int s;//左邊第幾排開始void dfs(int x, int y, char ch, int num){int nx, ny;for (int i=0; i<4; i++){nx = x + dir[i][0];ny = y + dir[i][1];if (map[nx][ny] == '0' || map[nx][ny] != ch || vis[nx][ny]) continue;vis[nx][ny] = num;maxnum[num] ++;dfs(nx, ny, ch, num);}}int main(){while (scanf("%d %d", &n, &m) != EOF){memset(map, '0', sizeof(map));getchar();s = 1;int i, j;for (i=1; i<=n; i++){for (j=1; j<=m; j++){char c = getchar();map[i][j] = c;}getchar();}int ans = 0;while (true){memset(vis, 0, sizeof(vis));memset(maxnum, 0, sizeof(maxnum));len = 1;for (i=1; i<=n; i++)for (j=s; j<=m; j++)if (vis[i][j] == 0 && map[i][j] != '0'){vis[i][j] = len;maxnum[len] ++;dfs(i, j, map[i][j], len);len ++;}int Max = 0, iMax = 0;for (i=1; i<len; i++)if (maxnum[i] > Max)Max = maxnum[i], iMax = i;if (Max <= 1) break;ans += Max * Max - Max;for (i=1; i<=n; i++)for (j=s; j<=m; j++)if (vis[i][j] == iMax)for (int k=i; map[k][j] != '0'; k--)map[k][j] = map[k-1][j];bool flag = true;while (flag){int mark = 0;for (int j=m; j>=s; j--){flag = true;for (int i=1; i<=n; i++)if (map[i][j] != '0')flag = false;if (flag){mark = j;break;}}if (flag){for (i=1; i<=n; i++)for (int k=mark; k >= s; k--)map[i][k] = map[i][k-1];s++;}if (m < 1 || s >= m) flag = false;}}printf("%d\n", ans);}return 0;}


範例輸入:

8 811223344551122334455112233445511223344551122334455112233445511229 91112223334445551112223334445551112223334445551112223334445551112223334445551112225 512345543211112222333123456 61122332223334444412211115552222222118 81234543212345321321452311354231123541351131354213135421231354132

範例輸出:

6416244122120



聯繫我們

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