-
Notifications
You must be signed in to change notification settings - Fork 0
/
23288.cpp
63 lines (63 loc) · 1.47 KB
/
23288.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <cstdio>
#include <cstring>
using namespace std;
int N,M,K,board[22][22],dice[7]={0,1,2,3,4,5,6},visit[22][22];
int dy[4]={0,1,0,-1};
int dx[4]={1,0,-1,0};
void move(int d){
int tmp[7];
for(int i=1;i<=6;i++) tmp[i]=dice[i];
if(d==0){
dice[1]=tmp[4];
dice[3]=tmp[1];
dice[4]=tmp[6];
dice[6]=tmp[3];
}else if(d==1){
dice[1]=tmp[2];
dice[2]=tmp[6];
dice[5]=tmp[1];
dice[6]=tmp[5];
}else if(d==2){
dice[1]=tmp[3];
dice[4]=tmp[1];
dice[3]=tmp[6];
dice[6]=tmp[4];
}else{
dice[1]=tmp[5];
dice[2]=tmp[1];
dice[5]=tmp[6];
dice[6]=tmp[2];
}
}
int dfs(int y,int x){
visit[y][x]=1;
int ret=1;
for(int i=0;i<4;i++){
int ny=y+dy[i],nx=x+dx[i];
if(board[ny][nx] && board[ny][nx]==board[y][x] && !visit[ny][nx]){
ret+=dfs(ny,nx);
}
}
return ret;
}
int main(){
scanf("%d%d%d",&N,&M,&K);
for(int i=1;i<=N;i++)
for(int j=1;j<=M;j++) scanf("%d",&board[i][j]);
int d=0,cy=1,cx=1,ans=0,cnt=0;;
while(K--){
int ny=cy+dy[d],nx=cx+dx[d];
if(!board[ny][nx]){
d=(d+2)%4;
K++;
continue;
}
move(d);
if(dice[6]>board[ny][nx]) d=(d+1)%4;
else if(dice[6]<board[ny][nx]) d=(d+3)%4;
ans+=dfs(ny,nx)*board[ny][nx];
memset(visit,0,sizeof(visit));
cy=ny,cx=nx;
}
printf("%d",ans);
}