-
Notifications
You must be signed in to change notification settings - Fork 167
/
Copy pathcoin-on-the-table.cpp
53 lines (47 loc) · 1.04 KB
/
coin-on-the-table.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
//coin-on-the-table.cpp
//Coin on the Table
//Algorithms - Search
//Author: derekhh
#include<iostream>
#include<cstring>
using namespace std;
char map[52][52];
int best[52][52][1001];
int n, m, k;
void update(int row, int col, int time, int val)
{
if (row < 0 || row >= n) return;
if (col < 0 || col >= m) return;
if (time > k) return;
if (val < best[row][col][time] || best[row][col][time] == -1)
{
best[row][col][time] = val;
if (map[row][col] != '*')
{
update(row, col + 1, time + 1, val + (map[row][col] != 'R'));
update(row, col - 1, time + 1, val + (map[row][col] != 'L'));
update(row - 1, col, time + 1, val + (map[row][col] != 'U'));
update(row + 1, col, time + 1, val + (map[row][col] != 'D'));
}
else
{
update(row, col, time + 1, val);
}
}
}
int main()
{
cin >> n >> m >> k;
int r, c;
for (int i = 0; i < n; i++)
for (int j = 0; j < m; j++)
{
cin >> map[i][j];
if (map[i][j] == '*')
r = i, c = j;
}
memset(best, -1, sizeof(best));
update(0, 0, 0, 0);
cout << best[r][c][k] << endl;
return 0;
}