답안 #22431

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
22431 2017-04-30T04:36:56 Z STARBUCKS(#950, hichoe95, exqt, kshy9598) Young Zebra (KRIII5_YZ) C++
0 / 7
0 ms 2144 KB
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <utility>
#include <string>
#include <set>
#include <map>
using namespace std;

char A[120][120];
int d[120][120];
int visit[120][120];

int dx[] = {-1, 0, 1, 0};
int dy[] = {0, 1, 0, -1};

int n, m; 
const int INF = 1207;

int dfs(int x, int y, char c)
{
	if(x == 0 || x == 3*n-1 || y == 0 || y == 3*m-1) return INF;
	if(visit[x][y]) return 0;
	if(d[x][y] != -2) return d[x][y];

	visit[x][y] = 1;
	int ret = 0;
	for(int i=0; i<4; i++)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];

		if(A[nx][ny] == c) ret += dfs(nx, ny, c);
	}
	// cout << x << ' ' << y << ' ' << ret + 1 << ' ' << endl;
	return d[x][y] = ret + 1;
}

void fill(int x, int y, char c, int ans)
{
	if(x == 0 || x == 3*n-1 || y == 0 || y == 3*m-1) return;
	if(visit[x][y] == 2) return;

	visit[x][y] = 2;
	d[x][y] = ans;

	for(int i=0; i<4; i++)
	{
		int nx = x + dx[i];
		int ny = y + dy[i];

		if(A[nx][ny] == c) fill(nx, ny, c, ans);
	}
}

int main()
{
	scanf("%d %d", &n, &m);
	for(int i=0; i<n; i++)
	{
		for(int j=0; j<m; j++)
		{
			scanf(" %c ", &A[i][j]);
			for(int ii=0; ii<3; ii++)
			{
				for(int jj=0; jj<3; jj++)
				{
					A[n*ii+i][m*jj+j] = A[i][j];
					d[n*ii+i][m*jj+j] = -2;
				}
			}
		}
	}

	// for(int i=0; i<3*n; i++)
	// {
	// 	for(int j=0; j<3*m; j++)
	// 	{
	// 		printf("%c", A[i][j]);
	// 	} printf("\n");
	// }

	for(int i=n; i<2*n; i++)
	{
		for(int j=m; j<2*m; j++)
		{
			int t = dfs(i, j, A[i][j]);
			for(int ii=0; ii<3; ii++)
			{
				for(int jj=0; jj<3; jj++)
				{
					fill((ii-1)*n+i, (jj-1)*m+j, A[i][j], t);
				}
			}

			printf("%d ", d[i][j] >= INF ? -1 : d[i][j]);
			// printf("%d ", t);
		} printf("\n");
	}
	
	return 0;
}

Compilation message

YZ.cpp: In function 'int main()':
YZ.cpp:59:24: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d %d", &n, &m);
                        ^
YZ.cpp:64:27: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
    scanf(" %c ", &A[i][j]);
                           ^
# 결과 실행 시간 메모리 Grader output
1 Runtime error 0 ms 2144 KB Execution killed with signal 11 (could be triggered by violating memory limits)
2 Halted 0 ms 0 KB -