poj1027 The Same Game (简单模拟题)
来源:互联网 发布:怎么恢复手机mac地址 编辑:程序博客网 时间:2024/06/11 14:24
一开始搜索部分用的是BFS,竟然MLE啦!谁能告诉我为什么?改成DFS后AC了。。。
题目要仔细阅读,否则会漏掉一些信息~~
/* * ===================================================================================== * * Filename: poj1027_The_Same_Game.cpp * * Description: 简单模拟题 * * Version: 1.0 * Created: 2012年02月24日 20时22分41秒 * Revision: none * Compiler: gcc * * Author: ahfywff (), ahfywff@163.com * Organization: * * ===================================================================================== */#include <iostream>#include <cstring>#include <cstdio>#include <queue>using namespace std;char map[20][20];bool vis[20][20];int leave, score;int rx, ry, rn, cnt;int dir[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};bool inMap(int x, int y){return (x >= 0 && x < 10 && y >= 0 && y < 15);}void dfs(int x, int y){vis[x][y] = true;cnt++;for (int i = 0; i < 4; ++i){int nx = x + dir[i][0];int ny = y + dir[i][1];if (inMap(nx, ny) && map[nx][ny] == map[x][y] && !vis[nx][ny]){dfs(nx, ny);}}}bool choose(){memset(vis, false, sizeof(vis));rn = 0;for (int i = 0; i < 15; ++i){for (int j = 9; j >= 0; --j){if (map[j][i] == ' ' || vis[j][i]) continue;cnt = 0;dfs(j, i);if (cnt > rn){rx = j; ry = i;rn = cnt;}}}return rn >= 2;}void remove(int x, int y){char color = map[x][y];map[x][y] = ' ';for (int i = 0; i < 4; ++i){int nx = x + dir[i][0];int ny = y + dir[i][1];if (inMap(nx, ny) && map[nx][ny] == color)remove(nx, ny);}}void shift(){for (int i = 0; i < 15; ++i){int pos = 9;for (int j = 9; j >= 0; --j){if (map[j][i] != ' ')map[pos--][i] = map[j][i];}while (pos >= 0){map[pos][i] = ' ';pos--;}}int pos = 0;for (int i = 0; i < 15; ++i){if (map[9][i] != ' '){for (int j = 0; j < 10; ++j){map[j][pos] = map[j][i];}pos++;}}while (pos < 15){for (int i = 0; i < 10; ++i)map[i][pos] = ' ';pos++;}}int main(){int t;scanf("%d", &t);for (int cas = 1; cas <= t; ++cas){leave = 10 * 15;score = 0;int step = 0;for (int i = 0; i < 10; ++i){scanf("%s", map[i]);}printf("Game %d:\n\n", cas);while (choose()){step++;printf("Move %d at (%d,%d): removed %d balls of color %c, got %d points.\n", step, 10 - rx, ry + 1, rn, map[rx][ry], (rn - 2) * (rn - 2));score += (rn - 2) * (rn - 2);leave -= rn;remove(rx, ry);shift();}if (leave == 0) score += 1000;printf("Final score: %d, with %d balls remaining.\n", score, leave);if (cas != t) printf("\n");}}
- poj1027 The Same Game (简单模拟题)
- poj1027--The Same Game(模拟)
- The Same Game-POJ1027模拟
- POJ1027--The Same Game
- poj1027 The Same Game
- POJ1027 The Same Game(AC)
- poj1027——The Same Game
- POJ 1027 The Same Game 模拟题
- poj 1027 The Same Game(大模拟)
- poj 1027 The Same Game 模拟
- poj1027 模拟
- poj1027 模拟
- 【模拟】POJ_1027_The Same Game
- POJ 1027 The Same Game棋盘游戏(模拟)
- CSUOJ 1862 The Same Game 模拟消消乐
- 文章标题CSU 1862: The Same Game(模拟)
- The Same Game
- The Same Game POJ
- logback的使用(1)
- OSPF协议配置
- HTML5是什么东东,为什么要关注它
- peipei
- 静态路由配置2
- poj1027 The Same Game (简单模拟题)
- CCSpriteBatchNode与"pvr.ccz","plist"精灵优化及注意事项
- 静态路由配置1
- Android app项目开发步骤总结
- 黑马程序员_Java开发前奏
- pkg-config的用法
- Myeclipse使用技巧
- 通过注册表来设置修改默认浏览器的方法
- 简单使用ADO测试数据源