Skip to content

Commit 724a4d6

Browse files
authored
Create 2019-C-A.cpp
hash table
1 parent 0f2c336 commit 724a4d6

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

Diff for: kick-start/2019-C-A.cpp

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#include<bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
#define ms(x,v) memset(x,(v), sizeof(x))
6+
#define msn(x,v,n) memset(x,(v),sizeof(x[0]) * n)
7+
#define INF 0x3f3f3f3f
8+
9+
typedef long long LL;
10+
typedef pair<int,int> PII;
11+
12+
const int MAXN = 100000 + 3;
13+
int n,r,c;
14+
int sr,sc;
15+
map<pair<int,int>,map<char,PII> > cell;
16+
set<PII> mat;
17+
PII next_cell(int x,int y,char dir){
18+
switch (dir)
19+
{
20+
case 'E':
21+
return make_pair(x,y+1);
22+
23+
case 'W':
24+
return make_pair(x,y-1);
25+
case 'N':
26+
return make_pair(x-1,y);
27+
case 'S':
28+
return make_pair(x+1,y);
29+
}
30+
}
31+
32+
pair<int,int > solve(int x,int y,char dir){
33+
auto cur_pos= make_pair(x,y);
34+
PII ret;
35+
if(cell.count(cur_pos) && cell[cur_pos].count(dir))
36+
{
37+
PII raw = cell[cur_pos][dir];
38+
ret = solve(raw.first,raw.second,dir);
39+
cell[cur_pos][dir] = ret;
40+
return ret;
41+
}
42+
if(!cell.count(cur_pos)){// bulid a empty map
43+
map<char,PII> tmp;
44+
cell[cur_pos] = tmp;
45+
}
46+
auto next_pos = next_cell(x,y,dir);
47+
if(!mat.count(next_pos)){
48+
mat.insert(next_pos);
49+
cell[cur_pos][dir] = next_pos;
50+
return next_pos;
51+
}else{
52+
cell[cur_pos][dir] = solve(next_pos.first,next_pos.second,dir);
53+
return cell[cur_pos][dir];
54+
}
55+
}
56+
57+
int main(int argc, char const *argv[])
58+
{
59+
ios :: sync_with_stdio(0);
60+
cin.tie(0);
61+
std::cout.precision(8);
62+
std::cout.setf( std::ios::fixed, std:: ios::floatfield );
63+
int T;
64+
cin >> T;
65+
66+
for(int t =1; t <= T ; ++t)
67+
{
68+
cin >>n >>r >> c >> sr >> sc;
69+
string s;
70+
cin >> s;
71+
mat.clear();
72+
mat.insert(make_pair(sr,sc));
73+
cell.clear();
74+
75+
auto cur = make_pair(sr,sc);
76+
for(auto e : s){
77+
cur = solve(cur.first,cur.second,e);
78+
}
79+
cout << "Case #"<<t << ": " << cur.first << " " << cur.second << "\n";
80+
}
81+
82+
return 0;
83+
}

0 commit comments

Comments
 (0)