AcWing1250。グリッドゲーム

AcWing1250。グリッドゲーム

アリスとボブは古いゲームをプレイしました。最初にn×nドットマトリックスを描画します(下の図ではn = 3)。

次に、2人が交代で、隣接するポイント間に赤と青の境界線を描画しました。

1.png

閉じた円が形成されるまで(面積は1である必要はありません)、「円を閉じた」人が勝者です。ボードが大きすぎるので、彼らのゲームは長すぎます!

彼らはゲームで誰がゲームに勝ったのかさえ知りません。

それで、彼らがゲームを終了したかどうかを計算するのを助けるプログラムを書いてください?

入力フォーマット

入力データの最初の行は、2つの整数nとmです。nはドットマトリックスのサイズを表し、mは合計m本の線が引かれたことを表します。

次のm行では、各行に最初に行の始点座標を表す2つの数字(x、y)があり、次に文字をスペースで区切ります。文字がDの場合、下に接続されます。 Rの場合は、片側を右に接続します。

入力データには重複するエッジがなく、正しいことが保証されています。

出力フォーマット

1行出力:最初の数ステップで終了します。

mステップ後に終了しない場合は、「draw」の行が出力されます。

データ範囲

1≤n≤2001≤n≤200 1n2 0 0
1≤M≤240001≤m≤240001m2 4 0 0 0

コード

#include<iostream>
#include<cstdio>
#include<queue>
#include<string>
#include<cstring>
#include<map>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
#include<vector>
#include<utility>
#include<deque>
#define INF 0x3f3f3f3f
#define mod 1000000007
#define endl '\n'
#define eps 1e-6
inline int gcd(int a, int b) {
    
     return b ? gcd(b, a % b) : a; }
inline int lowbit(int x) {
    
     return x & -x; }


using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
const int N = 50000;
int f[N];
int n,m;
int Find(int x) {
    
    
	return x == f[x] ? x : f[x] = Find(f[x]);
}

int get(int x, int y) {
    
    
	return x * n + y;
}

void merge(int a, int b) {
    
    
	f[Find(a)] = Find(b);
}

int main() {
    
    
	cin >> n >> m;

	for (int i = 0; i < N;++i)f[i] = i;
	int res = 0;
	for (int i = 1; i <= m;++i) {
    
    
		int x, y;
		char d;
		cin >> x >> y >> d;
		int a = get(x, y);
		int b = 0;
		if (d == 'D')b = get(x + 1, y);
		else b = get(x, y + 1);

		if (Find(a) != Find(b))merge(a, b);
		else {
    
    
			res = i;
			break;
		}
	}

	if (!res)puts("draw");
	else cout << res << endl;
	return 0;
}

おすすめ

転載: blog.csdn.net/zzq0523/article/details/113099536