Submission #22738

# Submission time Handle Problem Language Result Execution time Memory
22738 2017-04-30T07:08:48 Z past future present(#977, kazel, pjh0123, nemo) Young Zebra (KRIII5_YZ) C++14
0 / 7
366 ms 88352 KB
#include<stdio.h>
#include<vector>
#include<queue>
using namespace std;

char in[405];
bool fl[1200][1200];
int dx[4] = { -1,0,0,1 }, dy[4] = { 0,1,-1,0 }, n, m, nn, mm;
vector<int> g[1200 * 1200];

int ans[400 * 400], v[1200][1200], nf;

inline int pti(int y, int x)
{
	return y*mm + x;
}

int abs(int x)
{
	return x < 0 ? -x : x;
}

int foo(int i, int j)
{
	if (v[i][j] != 0) return ans[v[i][j]];
	nf++;
	v[i][j] = nf;
	queue<int> q;
	q.emplace(pti(i, j));
	while (!q.empty())
	{
		int idx = q.front();
		int y = idx / mm;
		int x = idx % mm;
		q.pop();
		if (ans[nf] >= 0) ans[nf]++;
		for (int e : g[idx])
		{
			int yy = e / mm, xx = e%mm;
			if (v[yy][xx]) continue;
			int yc = abs(yy - i), xc = abs(xx - j);
			if(yc%n == 0 && xc%m == 0)
			{
				ans[nf] = -1;
			}
			v[yy][xx] = nf;
			q.emplace(e);
		}
	}
	return ans[nf];
}

int main()
{
	scanf("%d%d", &n, &m);
	nn = n * 3;
	mm = m * 3;
	for (int i = 0; i<n; i++)
	{
		scanf("%s", in);
		for (int j = 0; j < m; j++)
		{
			fl[i][j] = fl[i][j + m] = fl[i][j + 2 * m] = in[j] == 'B';
			fl[i + n][j] = fl[i + n][j + m] = fl[i + n][j + 2 * m] = in[j] == 'B';
			fl[i + n * 2][j] = fl[i + n * 2][j + m] = fl[i + n * 2][j + 2 * m] = in[j] == 'B';
		}
	}
	for (int i = 0; i<nn; i++)
		for (int j = 0; j < mm; j++)
		{
			int p1 = pti(i, j);
			for (int d = 0; d < 4; d++)
			{
				int x = j + dx[d], y = i + dy[d];
				if (x < 0 || x >= 3 * m || y < 0 || y >= 3 * n) continue;
				if (fl[i][j] != fl[y][x]) continue;
				int p2 = pti(y, x);
				g[p1].push_back(p2);
			}
		}
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < m; j++)
		{
			printf("%d ", foo(i + n, j + m));
		}
		puts("");
	}
}

Compilation message

YZ.cpp: In function 'int foo(int, int)':
YZ.cpp:33:7: warning: unused variable 'y' [-Wunused-variable]
   int y = idx / mm;
       ^
YZ.cpp:34:7: warning: unused variable 'x' [-Wunused-variable]
   int x = idx % mm;
       ^
YZ.cpp: In function 'int main()':
YZ.cpp:55:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
                       ^
YZ.cpp:60:18: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
   scanf("%s", in);
                  ^
# Verdict Execution time Memory Grader output
1 Partially correct 366 ms 88352 KB Output is partially correct
2 Incorrect 359 ms 88352 KB Output isn't correct
3 Halted 0 ms 0 KB -