[BOJ] 1331 나이트 투어

Time Lapse :20min 53sec

1331.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
#include <stdio.h>
#include <stdlib.h>
char buf[3], nbuf[3], sbuf[3];
int visit[6][6];
int abs(int n){
int ret = buf[n] - nbuf[n];
ret = ret > 0 ? ret : -ret;
return ret == 1 || ret == 2 ? ret : 99;
}
int main(){
scanf("%s",buf);
if(buf[0]<'A'||buf[0]>'F'||buf[1]<'1'||buf[1]>'6'){
printf("Invalid");
exit(0);
}
sbuf[0] = buf[0], sbuf[1] = buf[1];
visit[buf[0]-'A'][buf[1]-'1'] = 1;
for(int i = 0; i <35; ++i){
scanf("%s",nbuf);
if(nbuf[0]<'A'||nbuf[0]>'F'||nbuf[1]<'1'||nbuf[1]>'6'||abs(0)+abs(1)!=3||visit[nbuf[0]-'A'][nbuf[1]-'1']){
printf("Invalid");
exit(0);
}
visit[nbuf[0]-'A'][nbuf[1]-'1'] = 1;
buf[0] = nbuf[0], buf[1] = nbuf[1];
}
nbuf[0] = sbuf[0], nbuf[1] = sbuf[1];
if(abs(0)+abs(1)!=3) printf("Invalid");
else printf("Valid");
}
Author: Song Hayoung
Link: https://songhayoung.github.io/2020/07/23/PS/BOJ/1331/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.