https://codeforces.com/contest/1695/problem/C
Problem - C - Codeforces
codeforces.com
정말 멘탈 많이 나갔었는데 최대값과 최소값만 관리하면 그 사이의 값은 모두 가능하다는 팁을 듣고
10분만에 ac...
멘탈 관리 열심히 해야것다.
import java.util.*;
import java.io.*;
public class q3 {
static FastScanner fs = new FastScanner();
static PrintWriter pw = new PrintWriter(System.out);
static int n, m;
static boolean[][] map;
static int[][][] ends;
public static void main(String[] args) {
int t = fs.nextInt();
while (t-- > 0){
n = fs.nextInt();
m = fs.nextInt();
map = new boolean[n][m];
ends = new int[n][m][2];
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (fs.nextInt() == 1){
map[i][j] = true;
}
}
}
if ((n + m) % 2 == 0){
pw.println("NO");
continue;
}
for (int i=0;i<n;i++){
for (int j=0;j<m;j++){
if (i == 0 && j == 0){
if (map[0][0]) ends[0][0][0] = ends[0][0][1] = 1;
else ends[0][0][0] = ends[0][0][1] = -1;
continue;
}
int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE, now = -1;
if (map[i][j]) now = 1;
if (i != 0){
max = ends[i - 1][j][0] + now;
min = ends[i - 1][j][1] + now;
}
if (j != 0){
max = Math.max(max, ends[i][j - 1][0] + now);
min = Math.min(min, ends[i][j - 1][1] + now);
}
ends[i][j][0] = max;
ends[i][j][1] = min;
}
}
int max = ends[n - 1][m - 1][0], min = ends[n - 1][m - 1][1];
if (max < 0 || min > 0) pw.println("NO");
else pw.println("YES");
}
pw.close();
}
// ----------input function----------
static void sort(int[] a) {
ArrayList<Integer> L = new ArrayList<>();
for (int i : a)
L.add(i);
Collections.sort(L);
for (int i = 0; i < a.length; i++)
a[i] = L.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
'PS > Problems' 카테고리의 다른 글
[Java] 백준 24041번 성싶당 밀키트 - 이분 탐색 (0) | 2022.06.09 |
---|---|
[Java] 백준 3015번 오아시스 재결합 - Stack (0) | 2022.05.19 |
[Java] 백준 1761번 정점들의 거리 - LCA 응용 (0) | 2022.05.17 |
[Java] 백준 11438번 LCA 2 - LCA (최소 공통 조상) (0) | 2022.05.17 |
[Java] 백준 13334번 철로 - 스위핑 (0) | 2022.04.30 |