結果

問題 No.43 野球の試合
ユーザー tempura-samatempura-sama
提出日時 2016-04-20 14:57:08
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,380 bytes
コンパイル時間 559 ms
コンパイル使用メモリ 58,404 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-15 03:53:39
合計ジャッジ時間 1,156 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 1 ms
6,940 KB
testcase_03 AC 2 ms
6,944 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 2 ms
6,940 KB
testcase_06 AC 2 ms
6,940 KB
testcase_07 AC 1 ms
6,944 KB
testcase_08 AC 1 ms
6,944 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
using namespace std;

#define N 6

int n;
char s[N][N];

int rank()
{
	int w[N] = { 0, };
	for ( int i = 0; i < n; i++ ) {
		for ( int j = 0; j < n; j++ ) {
			w[i] += s[i][j] == 'o' ? 1 : 0;
		}
	}

	int t[N] = { 0, };
	for ( int i = 0; i < n; i++ ) {
		t[w[i]] = 1;
	}

	int r = 0;
	for ( int i = w[0]; i < n; i++ ) {
		r += t[i];
	}

	return r;
}

int dfs(int a, int b, int r, int rg)
{
	s[a][b] = r ? 'o' : 'x';
	s[b][a] = r ? 'x' : 'o';

	int mn = N;
	if ( rg == 1 ) {
		mn = ::rank();
	}
	else {
		int t = b;
		for ( int i = a; i < n - 1; i++ ) {
			for ( int j = t + 1; j < n; j++ ) {
				if ( s[i][j] == '-' ) {
					int r1 = dfs(i, j, 1, rg - 1);
					if ( r1 == 1 ) { return 1; }
					int r2 = dfs(i, j, 0, rg - 1);
					if ( r2 == 1 ) { return 1; }
					mn = min(min(r1, r2), mn);
				}
			}
			t = i + 1;
		}
	}

	s[a][b] = s[b][a] = '-';

	return mn;
}

int main()
{
	int rg = 0;
	cin >> n;
	for ( int i = 0; i < n; i++ ) {
		for ( int j = 0; j < n; j++ ) {
			cin >> s[i][j];
			rg += s[i][j] == '-' ? 1 : 0;
		}
	}
	rg /= 2;

	if ( rg == 0 ) {
		cout << ::rank() << endl;
		return 0;
	}

	for ( int i = 0; i < n - 1; i++ ) {
		for ( int j = i + 1; j < n; j++ ) {
			if ( s[i][j] == '-' ) {
				int r1 = dfs(i, j, 1, rg);
				cout << (r1 == 1 ? 1 : min(r1, dfs(i, j, 0, rg))) << endl;
				return 0;
			}
		}
	}

	return 0;
}
0