格子游戏

来源:互联网 发布:b2b推广软件 编辑:程序博客网 时间:2024/06/10 11:55
Problem DescriptionAlice和Bob玩了一个古老的游戏:首先画一个n*n的点阵,接着他们两个轮流在相邻的点之间画上虚边和粗边,直到围成一个封闭的圈(面积不必为1)为止,“封圈”的那个人就是赢家。因为棋盘实在是太大了(n<=200),他们的游戏实在是长了!他们甚至在游戏中都不知道谁赢得了游戏。于是请你写一个程序,帮助他们计算他们是否结束了游戏?Input每组输入数据第一行为两个整数n和m。m表示一共画了m条线。以后m行,每行首先有两个数字(x,y),代表了画线的起点坐标,接着用空格隔开一个字符,假如字符是“D”,则是向下连一条边,如果是“R”就是向右连一条边。输入数据不会有重复的边且保证正确。Output对于每组数据,输出一行:在第几步的时候结束。假如m步之后也没有结束,则输出一行“draw”。Sample Input3 51 1 D1 1 R1 2 D2 1 R 2 2 DSample Output4//关键字:二维的并查集.//标程: #include <iostream> #include <cstdio> #include <cstring> using namespace std; struct node {     int x, y; }f[330][330]; node findy(int x,int y) {     if(f[x][y].x == x && f[x][y].y == y) return f[x][y];     f[x][y] = findy(f[x][y].x,f[x][y].y);     return f[x][y]; } int main() { //  freopen("a.txt","r",stdin);     int n, m, i, j;     while(cin >> n >> m)     {         for(i = 1; i <= n; ++ i)            for(j = 1; j <= n; ++ j)                f[i][j].x = i, f[i][j].y = j;         int x, y, flag = 0;         char ch;         for(i = 1; i <= m; ++ i)         {             cin >> x >> y >> ch;             if(flag)  continue;             node a = findy(x,y), b;             if(ch == 'D')                b = findy(x+1,y);             else b = findy(x,y+1);             if(a.x == b.x && a.y == b.y)             {                 cout << i << endl;                 flag = 1;             }             else  f[b.x][b.y] = a;         }         if(flag == 0) cout << "draw" << endl;     }     return 0; }

0 0
原创粉丝点击