CodeForces 3C Tic-tac-toe(模拟)
2016-03-28 22:17
477 查看
题意:XO游戏,现在给你一局游戏,让你判断是否合法,谁获胜,如果还没获胜,则输出下一步由谁走
思路:模拟..细节很多
Description
Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts).
The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is
announced.
You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:
illegal — if the given board layout can't appear during a valid game;
the first player won — if in the given board layout the first player has just won;
the second player won — if in the given board layout the second player has just won;
draw — if the given board layout has just let to a draw.
Input
The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero).
Output
Print one of the six verdicts: first, second, illegal, the first player won, the
second player won or draw.
Sample Input
Input
Output
思路:模拟..细节很多
#include<bits/stdc++.h> using namespace std; string s[3]; int check(char c) { for(int i=0;i<3;i++) { int win = 1; for(int j=0;j<3;j++) if(s[i][j]!=c) win=0; if(win==1)return win; } for(int i=0;i<3;i++) { int win=1; for(int j=0;j<3;j++) if(s[j][i]!=c) win=0; if(win==1)return win; } if(s[0][0]==c&&s[1][1]==c&&s[2][2]==c)return 1; if(s[0][2]==c&&s[1][1]==c&&s[2][0]==c)return 1; return 0; } int main() { for(int i=0;i<3;i++) cin>>s[i]; int X=0,O=0; for(int i=0;i<3;i++) for(int j=0;j<3;j++) if(s[i][j]=='X')X++; else if(s[i][j]=='0')O++; int Win1 = check('X'); int Win2 = check('0'); if(Win1&&Win2)return puts("illegal"),0; if(Win1&&X-O!=1)return puts("illegal"),0; if(Win2&&X!=O)return puts("illegal"),0; if(X-O>1||O>X)return puts("illegal"),0; if(Win1)return puts("the first player won"),0; if(Win2)return puts("the second player won"),0; if(X+O==9)return puts("draw"),0; if(X==O)return puts("first"),0; if(O==X-1)return puts("second"),0; }
Description
Certainly, everyone is familiar with tic-tac-toe game. The rules are very simple indeed. Two players take turns marking the cells in a 3 × 3 grid (one player always draws crosses, the other — noughts).
The player who succeeds first in placing three of his marks in a horizontal, vertical or diagonal line wins, and the game is finished. The player who draws crosses goes first. If the grid is filled, but neither Xs, nor 0s form the required line, a draw is
announced.
You are given a 3 × 3 grid, each grid cell is empty, or occupied by a cross or a nought. You have to find the player (first or second), whose turn is next, or print one of the verdicts below:
illegal — if the given board layout can't appear during a valid game;
the first player won — if in the given board layout the first player has just won;
the second player won — if in the given board layout the second player has just won;
draw — if the given board layout has just let to a draw.
Input
The input consists of three lines, each of the lines contains characters ".", "X" or "0" (a period, a capital letter X, or a digit zero).
Output
Print one of the six verdicts: first, second, illegal, the first player won, the
second player won or draw.
Sample Input
Input
X0X .0. .X.
Output
second
相关文章推荐
- STL map
- 一次发现underscore源码bug的经历以及对学术界拿来主义的思考
- 《Linux内核设计与实现》课本第十八章自学笔记——20135203齐岳
- 【匈牙利算法】BZOJ1059-[ZJOI2007]矩阵游戏
- java 内存举例
- 板载RAID
- python 实现下载图片
- LVS 三种模式区别
- 整理hibernate 的set属性
- jumpserver 堡垒机环境搭建(图文详解)
- 阿里面试算法题-1-100随机替换两个为零,如何快速找出
- iOS开发之delegate设计模式
- Python基础知识:构造函数中self用法
- Java concurrentModificationException
- scala(2) 变长参数 lazy 异常处理 数组Array Map tuple
- Ansible11:变量详解
- jQuery中filter() 方法的使用
- bzoj 3240: [Noi2013]矩阵游戏
- Bios工程师手边事—PCI资源分配
- maven仓库--私服(Nexus的配置使用)