小程序
2013-10-28 11:02
134 查看
#include <iostream> using namespace std; //enum Type {AIR, WHITE, BLACK}; int A[10][10] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; bool B[10][10]; int eatCount = 0; void initFlagMatrix() { for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) B[i][j] = false; } } bool hasAir(int i, int j, int type) { if(A[i][j] == 0) return true; if(A[i][j] != type) return false; eatCount++; B[i][j] = true; if(i > 0 && !B[i-1][j] && hasAir(i-1, j, type)) return true; else if(i < 9 && !B[i+1][j] && hasAir(i+1, j, type)) return true; else if(j > 0 && !B[i][j-1] && hasAir(i, j-1, type)) return true; else if(j < 9 && !B[i][j+1] && hasAir(i, j+1, type)) return true; else return false; } //将与A[i][j]相连的相同类型的棋子全部吃掉 void eatChess(int i, int j, int type) { if(A[i][j] != type) return; A[i][j] = 0; //eat the chess if(i > 0) eatChess(i-1, j, type); if(i < 9) eatChess(i+1, j, type); if(j > 0) eatChess(i, j-1, type); if(j < 9) eatChess(i, j+1, type); } //check the whole chess of type is there any chess if out of air bool hasAirOfType(int type, int &p, int &q) { for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { if(A[i][j] != type || B[i][j]) continue; eatCount = 0; if(!hasAir(i, j, type)) { p = i, q = j; return false; } } } return true; } //get the chess that has been eaten when we put a chess of type on position [i,j] int eatenChesscount(int i, int j, int type) { initFlagMatrix(); bool self_hasAir = hasAir(i, j, type); eatCount = 0; int p = 0, q = 0; int other_type = (type==1?2:1); initFlagMatrix(); bool other_hasAir = hasAirOfType(other_type, p, q); if(!self_hasAir && other_hasAir) { A[i][j] = 0; return INT_MAX; //suicide chess is not allowed } if(!other_hasAir) { eatChess(p, q, other_type); if(other_type == 1) return eatCount; else return 0-eatCount; } return 0; } void printChessState() { for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { cout << A[i][j] << " "; } cout << endl; } } int main() { int i, j, type; while(cin >> i >> j >> type) { if(A[i][j] != 0) { cout << INT_MAX << endl; continue; } A[i][j] = type; printChessState(); cout << eatenChesscount(i, j, type) << endl; printChessState(); } return 0; }
相关文章推荐
- 程序内存空间
- 用Visual C#创建Windows服务程序
- 一个杀进程的小程序的c#源程序
- 自己写的一个在线升级的程序!
- 第一個windows forms2.0數據綁定應用程序
- 加密程序的另一个用法
- ASP.NET程序中常用的三十三种代码 (五)
- 09-11-09十进制转二进制、八进制、十六进制的经典程序
- 甲骨文发布Java补丁程序 修复27处漏洞
- 有关Linux程序优先级的问题
- c# 调用控制台程序并传参 获取控制台输出(解释器的前台处理)
- android eclipse开发环境 自动提示 程序无法响应解决方法
- c# 好程序(自己收藏,未整理)
- C#获取当前程序运行路径的方法集合
- android 4.0 某些APK程序无法安装的解决记录
- 使用SQL数据提供程序访问MSSQL数据库
- 如何在Android中启动JAVA程序
- 如何在windows上使用eclipse远程连接hadoop进行程序开发
- 程序中变量存储类别
- 程序算法与人生选择