Submission #02399


ソースコード

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
#include<bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const int dy[] = {-1, 0, 0, 1}, dx[] = {0, -1, 1, 0};
int H, W;
string S[1000];
short dp[1000][1000][1 << 7];
bool isOver(int x, int y)
{
return(x < 0 || y < 0 || x >= W || y >= H);
}
bool isPoyo(int x, int y)
{
return(!isOver(x, y) && isdigit(S[y][x]));
}
inline int MvR(int used) // 右に移動
{
return(28 | ((used >> 4) & 1)| (((used >> 2) & 3) << 5));
}
inline int MvD(int used)
{
return(76 | (((used >> 6) & 1) << 1) | (((used >> 2) & 3) << 4));
}
int rec(int x, int y, int used)
{
if(~dp[x][y][used]) return(dp[x][y][used]);
if(x == W - 1 && y == H - 1) return(0);
int bit = 0, cost = 0;
for(int i = 0; i < 4; i++) {
if(isPoyo(x + dx[i], y + dy[i]) & (used >> i)) {
cost += S[y + dy[i]][x + dx[i]] - '0';
bit |= 1 << i;
}
}
int ret = INF, base = (isdigit(S[y][x]) & (used >> 5) ? S[y][x] - '0' : 0);
if(bit == 0) {
if(x + 1 < W) ret = min(ret, rec(x + 1, y, MvR(used)) + cost);
if(y + 1 < H) ret = min(ret, rec(x, y + 1, MvD(used)) + cost);
} else {
for(int i = 0; i < 4; i++) {
if((bit >> i) & 1) {
bit &= ~(1 << i);
cost -= S[y + dy[i]][x + dx[i]] - '0';
if(x + 1 < W) ret = min(ret, rec(x + 1, y, MvR(used & ~bit)) + cost);
if(y + 1 < H) ret = min(ret, rec(x, y + 1, MvD(used & ~bit)) + cost);
cost += S[y + dy[i]][x + dx[i]] - '0';
bit |= (1 << i);
}
}
}
return(dp[x][y][used] = ret + base);
}
int main()
{
memset(dp, -1, sizeof(dp));
cin >> H >> W;
for(int i = 0; i < H; i++) cin >> S[i];
cout << rec(0, 0, (1 << 7) - 1) << endl;
}

ステータス

項目 データ
問題 0262 - 屋台 (Food stalls)
ユーザー名 ei1333
投稿日時 2015-12-16 18:17:02
言語 C++11
状態 Accepted
得点 5
ソースコード長 1723 Byte
最大実行時間 509 ms
最大メモリ使用量 252668 KB

セット

セット 得点 Cases
1 INPUT1 1 / 1 *in1.txt
2 INPUT2 1 / 1 *in2.txt
3 INPUT3 1 / 1 *in3.txt
4 INPUT4 1 / 1 *in4.txt
5 INPUT5 1 / 1 *in5.txt

テストケース

ファイル名 状態 実行時間 メモリ使用量 #
2016-yo-t6-in1.txt AC 57 ms 250456 KB
1
2016-yo-t6-in2.txt AC 62 ms 250516 KB
2
2016-yo-t6-in3.txt AC 502 ms 252660 KB
3
2016-yo-t6-in4.txt AC 509 ms 252660 KB
4
2016-yo-t6-in5.txt AC 505 ms 252668 KB
5
2016-yo-t6-in_s1.txt AC 49 ms 250624 KB
2016-yo-t6-in_s2.txt AC 47 ms 250556 KB