-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquestion2.cpp
More file actions
94 lines (79 loc) · 1.73 KB
/
question2.cpp
File metadata and controls
94 lines (79 loc) · 1.73 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <bits/stdc++.h>
using namespace std;
//All Typedef's are here.
typedef long long ll;
typedef vector<ll> vll;
typedef vector<int> vi;
typedef set<int> si;
typedef set<ll> sll;
typedef vi::iterator viI;
typedef sll::iterator sllI;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef map<int, int> mii;
// typedef vector<vector<int>&> vvi;
//input file and outfile
ifstream yyy("input.txt");
ofstream zzz("output.txt");
void get_next_position(int x, int y, int direction, pii& ans)
{
if (direction == 0)
{
ans.first = x;
ans.second = y + 1;
}
else if (direction == 1)
{
ans.first = x + 1;
ans.second = y;
}
else if (direction == 2)
{
ans.first = x;
ans.second = y - 1;
}
else
{
ans.first = x - 1;
ans.second = y;
}
}
int main()
{
int n, m;
yyy >> n >> m;
// vvi array;
vector<vector<int>>array(n, vector<int>(m));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
yyy >> array[i][j];
}
}
int entry_x = 0, entry_y = 0;
bool flag = false;
int check_x, check_y;
int direction = 0;
int temp_x ;
int temp_y ;
while (!flag)
{
// looking in the east direction first.
pii pos;
get_next_position(entry_x, entry_y, direction, pos);
temp_x = pos.first;
temp_y = pos.second;
if(temp_x < 0 || temp_x >= n || temp_y < 0 || temp_y >= m)
{
zzz << entry_x << entry_y;
exit(0);
}
if(array[temp_x][temp_y] == 1)
{
direction = (direction+1)%4;
}
entry_x = temp_x;
entry_y = temp_y;
}
}