Submission #1140724


Source Code Expand

import java.util.ArrayList;
import java.util.Scanner;


public class Main {
	
	public static final int[][] move_dir = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};
	
	public static boolean in_range(int x, int y, int W, int H){
		return 0 <= x && x < W && 0 <= y && y < H;
	}
	
	public static boolean dfs(final int deep, final int limit, int x, int y, int W, int H, int[][] board, boolean[][] used, StringBuilder answer){
		if(deep >= limit){
			used[y][x] = true;
			answer.append((y + 1) + " " + (x + 1) + "\n");
			return true;
		}
		
		for(final int[] move : move_dir){
			final int nx = x + move[0];
			final int ny = y + move[1];
			
			if(!in_range(nx, ny, W, H)){
				continue;
			}else if(used[ny][nx]){
				continue;
			}else if(board[ny][nx] == 0){
				continue;
			}
			
			used[y][x] = true;
			if(dfs(deep + 1, limit, nx, ny, W, H, board, used, answer)){
				answer.append((y + 1) + " " + (x + 1) + "\n");
				return true;
			}
			
			used[y][x] = false;
		}
		
		return false;
	}
	
	public static void main(String[] args){
		try(final Scanner sc = new Scanner(System.in)){
			final int H = sc.nextInt();
			final int W = sc.nextInt();
			final int K = sc.nextInt();
			
			int[][] board = new int[H][W];
			for(int i = 0; i < H; i++){
				final char[] input = sc.next().toCharArray();
				
				for(int j = 0; j < W; j++){
					board[i][j] = Character.getNumericValue(input[j]);
				}
			}
			
			ArrayList<String> answers = new ArrayList<String>();
			boolean[][] used = new boolean[H][W];
			
			LOOP:
			while(true){
				boolean updated = false;
				
				for(int y = 0; y < H; y++){
					for(int x = 0; x < W; x++){
						if(!used[y][x] && board[y][x] != 0){
							final StringBuilder answer = new StringBuilder();
							final boolean ok = dfs(0, K - 1, x, y, W, H, board, used, answer);
							
							if(ok){
								answers.add(answer.toString());
								//System.out.println(answer);
								updated = true;
								continue LOOP;
							}
						}
					}
				}
				
				if(!updated){
					break;
				}
			}
			
			System.out.println(answers.size());
			for(final String str : answers){
				System.out.print(str);
			}
			System.out.flush();
		}
	}
}

Submission Info

Submission Time
Task A - Multiple Pieces
User tshirakawa
Language Java8 (OpenJDK 1.8.0)
Score 94410
Code Size 2257 Byte
Status AC
Exec Time 176 ms
Memory 28920 KB

Judge Result

Set Name test_01 test_02 test_03 test_04 test_05 test_06 test_07 test_08 test_09 test_10
Score / Max Score 8731 / 1343058 9713 / 1343058 11873 / 1343058 7089 / 1343058 8855 / 1343058 9606 / 1343058 10552 / 1343058 8855 / 1343058 8060 / 1343058 11076 / 1343058
Status
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
AC × 1
Set Name Test Cases
test_01 subtask_01_01.txt
test_02 subtask_01_02.txt
test_03 subtask_01_03.txt
test_04 subtask_01_04.txt
test_05 subtask_01_05.txt
test_06 subtask_01_06.txt
test_07 subtask_01_07.txt
test_08 subtask_01_08.txt
test_09 subtask_01_09.txt
test_10 subtask_01_10.txt
Case Name Status Exec Time Memory
subtask_01_01.txt AC 176 ms 27928 KB
subtask_01_02.txt AC 158 ms 25356 KB
subtask_01_03.txt AC 151 ms 26956 KB
subtask_01_04.txt AC 147 ms 26572 KB
subtask_01_05.txt AC 150 ms 25548 KB
subtask_01_06.txt AC 146 ms 25880 KB
subtask_01_07.txt AC 147 ms 28920 KB
subtask_01_08.txt AC 161 ms 25464 KB
subtask_01_09.txt AC 153 ms 26872 KB
subtask_01_10.txt AC 147 ms 25792 KB