[SWEA] 1210 Ladder1

Time Lapse :5min 3sec

1210.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
#define gc() getchar_unlocked()
inline void fastreadINT_byReference(short &ret){
int N = gc(), flag = 1;
for(;N<'0'||N>'9';N=gc())
if(N=='-'){
flag=-1; N=gc(); break;
}
ret = 0;
do{
ret = (ret<<3) + (ret<<1) + (N & 0b1111); N=gc();
}while('0'<=N&&N<='9');
ret *= flag;
}
int main() {
short tc, T = 10, y, x, dir;
short map[100][100], dx[2] = {-1, 1};
while(T--) {
fastreadINT_byReference(tc);
y = dir = 99;
for(int i = 0; i < 100; ++i)
for(int j = 0; j < 100; ++j)
fastreadINT_byReference(map[i][j]);

for(x = 0; map[y][x] != 2; ++x);

while(y) {
if(dir != 99 && 0 <= x + dx[dir] && x + dx[dir] < 99 && map[y][x + dx[dir]])
x += dx[dir];
else if(0 != x && map[y][x - 1] != 0 && dir != 1)
--x, dir = 0;
else if(99 != x && map[y][x + 1] != 0 && dir != 0)
++x, dir = 1;
else if(map[y - 1][x] == 1)
--y, dir = 99;
}
printf("#%d %d\n",tc, x);
}
}

1210.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import java.util.*;

public class Solution {
public static void main(String[] args) throws Exception {
Scanner scanner = new Scanner(System.in);
int tc = 1, T = 10, y, x, dir;
int[] dx = {0, 1, 0, -1};
int[] dy = {-1, 0, 1, 0};
int[][] map = new int[100][100];
while(T-- != 0) {
y = dir = 99;
tc = scanner.nextInt();
for(int i = 0; i < 100; ++i)
for(int j = 0; j < 100; ++j)
map[i][j] = scanner.nextInt();

for(x = 0; map[99][x] != 2; ++x);

for(;y != 0;) {
if((dir == 1 || dir == 3) && 0 <= x + dx[dir] && x + dx[dir] < 99 && map[y][x + dx[dir]] == 1) {
x = x + dx[dir];
}
else if(0 != x && map[y][x - 1] != 0 && dir != 1){
x = x - 1;
dir = 3;
}
else if(99 != x && map[y][x + 1] != 0 && dir != 3){
x = x + 1;
dir = 1;
}
else if(map[y - 1][x] == 1) {
y = y - 1;
dir = -1;
}
}
System.out.println("#" + tc + " " + x);
}
}
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/08/04/PS/SWEA/1210/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.