• 博客园logo
  • 会员
  • 周边
  • 新闻
  • 博问
  • 闪存
  • 众包
  • 赞助商
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
HaibaraAi
博客园    首页    新随笔    联系   管理    订阅  订阅

Warm up 13 [G] Game of Tiles

Game of Tiles
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB
Total submit users: 3, Accepted users: 2
Problem 12754 : No special judgement

Problem description

染色啊,什么的- -,对每个连通块进行黑白染色,构二分图,如果有一个连通分量不是完美匹配就输出1否则输出2!

The Game of Tiles is a game for two players played over a rectangular board in the form of a table of R rows and C columns of square cells called tiles. At the beginning of the game, some of the tiles may be painted black and the rest remain white. Then, Player 1 and Player 2 alternate turns making a move and the first one that cannot make a valid move loses the game. The first move of the game is done by Player 1 and consists of choosing a white tile and writing the number 1 on it. After that, each subsequent move i consists of writing number i on an unused white tile that is adjacent horizontally or vertically (but not diagonally) to the tile numbered i - 1. Note that Player 1 always writes odd numbers and Player 2 always writes even numbers. The following figure shows three examples of possible configurations of a board with R = 3 and C = 4 during a game. On the left it shows the initial configuration. On the center it shows an intermediate state, where cells in gray mark the possible moves for Player 2. And on the right it shows the configuration when the game is won by Player 2, who chose the appropriate move.
Your task is to write a program that given the initial configuration of the board, determines which player will win, if both of them play optimally.
Input
Each test case is described using several lines. The first line contains two integers R and C representing respectively the number of rows and columns of the board (1 ≤ R;C ≤ 50). The i-th of the next R lines contains a string Bi of C characters that describes the i-th row of the initial board. The j-th character of Bi is either "." (dot) or the uppercase letter "X", representing that the tile at row i and column j is respectively white or black. Within each test case at least one of the tiles is white. 
Output
For each test case output a line with an integer representing the number of the player (1 or 2) who will win the game if both of them play optimally.
Sample Input
3 4
....
XX.X
...X
3 4
....
.X.X
...X
3 4
....
.X.X
....
1 1
.
1 11
....X......
Sample Output
2
1
1
1
2
Problem Source
Latin American 2012
Submit   Discuss   Judge Status  Problems  Ranklist 
 1 #include <cstdio>
 2 #include <cstring>
 3 #include <algorithm>
 4 using namespace std;
 5 #define maxn 5005
 6 int n, m, id, w, b;
 7 int vis[51][51];
 8 char s[51][51];
 9 int head[2501], match[1251],check[1251];
10 struct edge{
11     int v, next;
12 }edges[maxn];
13 void add(int u, int v){
14     edges[id].v = v; edges[id].next = head[u]; head[u] = id++;
15 }
16 int dx [] = { 1, 0, -1, 0 };
17 int dy [] = { 0, 1, 0, -1 };
18 int dfs(int u){
19     for (int i = head[u]; i != -1; i = edges[i].next){
20         int v = edges[i].v;
21         if (!check[v]){
22             check[v] = 1;
23             if (match[v] == -1 || dfs(match[v])){
24                 match[v] = u;
25                 return 1;
26             }
27         }
28     }
29     return 0;
30 }
31 int km(){
32     int ans = 0;
33     memset(match, -1, sizeof match);
34     for (int i = 0; i < w; i++){
35         memset(check, 0, sizeof check);
36         if (dfs(i + 1))ans++;
37     }
38     return ans == w;
39 }
40 void dfs2(int x, int y, int c, int u){
41     for (int i = 0; i < 4; i++){
42         int nx = x + dx[i];
43         int ny = y + dy[i];
44         if (nx < 0 || ny < 0 || nx >= n || ny >= m || s[nx][ny] == 'X')continue;
45         if (c == 0){
46             if (vis[nx][ny]){
47                 add(u, vis[nx][ny]);
48                 continue;
49             }
50             add(u, b);
51             vis[nx][ny] = b;
52             dfs2(nx, ny, 1, b++);
53         }
54         else{
55             if (vis[nx][ny])continue;
56             vis[nx][ny] = w;
57             dfs2(nx, ny, 0, w++);
58         }
59     }
60 }
61 int solve(int x, int y){
62     w = 2; b = 1;
63     memset(head, -1, sizeof head);
64     vis[x][y] = 1;
65     dfs2(x, y, 0, 1);
66     w--; b--;
67     if (w == b)return km();
68     return 0;
69 }
70 int main(){
71     while (~scanf("%d%d", &n, &m)){
72         memset(vis, 0, sizeof vis);
73         id = 0;
74         int flag = 0;
75         for (int i = 0; i < n; i++)scanf("%s", s[i]);
76         for (int i = 0; i < n;i++)
77         for (int j = 0; j < m; j++){
78             if (s[i][j]=='X'||vis[i][j])continue;
79             if (solve(i, j) == 0){ flag = 1; i = n; j = m; }
80         }
81         printf("%d\n", flag ? 1 : 2);
82     }
83     return 0;
84 }
View Code
posted @ 2013-12-11 04:05  HaibaraAi  阅读(119)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2026
浙公网安备 33010602011771号 浙ICP备2021040463号-3