Submission #2532185


Source Code Expand

import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;

public class Main {

	static final int[] dx = { 1, 0, -1, 0 };
	static final int[] dy = { 0, 1, 0, -1 };
	static final int MOD = 1000000007;

	static int h, w;
	static int[][] a, memo;

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		h = sc.nextInt();
		w = sc.nextInt();
		a = new int[h + 2][w + 2];
		for (int y = 1; y <= h; y++) {
			for (int x = 1; x <= w; x++) {
				a[y][x] = sc.nextInt();
			}
		}

		int sum = 0;
		memo = new int[h + 2][w + 2];
		for (int y = 1; y <= h; y++) {
			for (int x = 1; x <= w; x++) {
				sum += dp(y, x);
				sum %= MOD;
			}
		}

		System.out.println(sum);

	}

	static int dp(int y, int x) {
		if (memo[y][x] > 0) return memo[y][x];
		int result = 1;
		for (int i = 0; i < 4; i++) {
			int ny = y + dy[i];
			int nx = x + dx[i];
			if (a[y][x] < a[ny][nx]) result = (result + dp(ny, nx)) % MOD;
		}
		return memo[y][x] = result;
	}

}

class Scanner {

	private final InputStream in;
	private final byte[] buffer;
	private int ptr;
	private int buflen;

	public Scanner(InputStream in) {
		this.in = in;
		this.buffer = new byte[1024];
		this.ptr = 0;
		this.buflen = 0;
	}

	private boolean hasNextByte() {
		if (ptr < buflen)
			return true;
		else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0)
				return false;
		}
		return true;
	}

	private byte readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		return -1;
	}

	private boolean isPrintableChar(byte c) {
		return '!' <= c && c <= '~';
	}

	private void skipUnprintable() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
			ptr++;
		}
	}

	public boolean hasNext() {
		skipUnprintable();
		return hasNextByte();
	}

	public int nextInt() {
		if (!hasNext())
			throw new NoSuchElementException();
		int n = 0;
		boolean minus = false;
		byte b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b)
			throw new NumberFormatException();
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}

}

Submission Info

Submission Time
Task D - 経路
User deka0106
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 2483 Byte
Status AC
Exec Time 196 ms
Memory 31456 KB

Judge Result

Set Name sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 24
Set Name Test Cases
sample sample01.txt, sample02.txt
All 00.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, sample01.txt, sample02.txt, sample01.txt, sample02.txt
Case Name Status Exec Time Memory
00.txt AC 174 ms 31208 KB
01.txt AC 191 ms 30016 KB
02.txt AC 148 ms 27804 KB
03.txt AC 68 ms 20308 KB
04.txt AC 69 ms 18004 KB
05.txt AC 71 ms 20820 KB
06.txt AC 74 ms 19664 KB
07.txt AC 74 ms 18516 KB
08.txt AC 69 ms 18900 KB
09.txt AC 70 ms 18772 KB
10.txt AC 74 ms 21076 KB
11.txt AC 196 ms 30560 KB
12.txt AC 192 ms 28256 KB
13.txt AC 192 ms 29176 KB
14.txt AC 193 ms 28512 KB
15.txt AC 192 ms 30320 KB
16.txt AC 196 ms 31096 KB
17.txt AC 193 ms 31456 KB
18.txt AC 154 ms 28828 KB
19.txt AC 132 ms 28404 KB
sample01.txt AC 70 ms 20948 KB
sample02.txt AC 69 ms 19152 KB