Skip to content

Commit c901c10

Browse files
committed
Data Structure Queue
1 parent e2bcff2 commit c901c10

File tree

1 file changed

+93
-0
lines changed

1 file changed

+93
-0
lines changed

cpp/3190.cpp

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
#include <queue>
5+
6+
using namespace std;
7+
8+
int board[101][101];
9+
bool Is_There_Snake[101][101];
10+
11+
int dy[4] = { 0,1,0,-1 };
12+
int dx[4] = { 1,0,-1,0 };
13+
14+
15+
char swift[10001];
16+
17+
int main()
18+
{
19+
ios_base::sync_with_stdio(false);
20+
cin.tie(NULL);
21+
cout.tie(NULL);
22+
23+
int board_Size,Apple_Cnt; cin >> board_Size >> Apple_Cnt;
24+
25+
for (int i = 0; i < Apple_Cnt; i++)
26+
{
27+
int col, row; cin >> col >> row;
28+
board[col][row] = 1;
29+
}
30+
31+
int swift_Cnt; cin >> swift_Cnt;
32+
33+
for (int i = 0; i < swift_Cnt; i++)
34+
{
35+
int swift_Time;
36+
char Direction;
37+
cin >> swift_Time >> Direction;
38+
swift[swift_Time] = Direction;
39+
}
40+
41+
queue<pair<int,int>> snake;
42+
43+
snake.push({ 1,1 });
44+
Is_There_Snake[1][1] = true;
45+
46+
int answer_Time = 0;
47+
int now_Direction = 0;
48+
49+
while (1)
50+
{
51+
answer_Time++;
52+
53+
int next_col = snake.back().first + dy[now_Direction];
54+
int next_row = snake.back().second + dx[now_Direction];
55+
56+
if (next_col < 1 || next_col > board_Size || next_row < 1 || next_row > board_Size) break;
57+
58+
snake.push({ next_col,next_row });
59+
60+
if (swift[answer_Time] == 'D')
61+
{
62+
now_Direction += 1;
63+
if (now_Direction == 4) now_Direction = 0;
64+
}
65+
66+
else if (swift[answer_Time] == 'L')
67+
{
68+
now_Direction -= 1;
69+
if (now_Direction == -1) now_Direction = 3;
70+
}
71+
72+
if (board[next_col][next_row] == 1)
73+
{
74+
board[next_col][next_row] = 0;
75+
}
76+
77+
else
78+
{
79+
if (Is_There_Snake[next_col][next_row] == true) break;
80+
Is_There_Snake[snake.front().first][snake.front().second] = false;
81+
snake.pop();
82+
}
83+
84+
if (Is_There_Snake[next_col][next_row] == true) break;
85+
86+
else Is_There_Snake[next_col][next_row] = true;
87+
88+
}
89+
90+
cout << answer_Time << endl;
91+
92+
return 0;
93+
}

0 commit comments

Comments
 (0)