Always WA On test 4
This is my program.
I'm always getting WA on test 4. I really don't know why.
If you know, send an E-mail to Sunzheng.david@163.com, or just give a reply.
Thank you very much.
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
int const dir_x[4] = {0, 1, 0, -1};
int const dir_y[4] = {1, 0, -1, 0};
char const dis_c[4] = {'E', 'S', 'W', 'N'};
typedef long double lld;
bool vis[10][10], found;
int w[20][4][4], max_w[20][4][4][4][4][20], dp[20][300][4][4], link[20][4][4], last[20][300][4][4][4];
char out[500];
int n, size = -1;
void search(int &s_floor, int &s_x, int &s_y, int x, int y, int t){
vis[x][y] = true;
for (int i = 0; i < 4; i ++){
int new_x = x + dir_x[i], new_y = y + dir_y[i];
if (new_x >= 0 && new_x < 4 && new_y >= 0 && new_y < 4 && !vis[new_x][new_y]){
int new_w = max_w[s_floor][s_x][s_y][x][y][t] + w[s_floor][new_x][new_y];
if (new_w > max_w[s_floor][s_x][s_y][new_x][new_y][t + 1]){
max_w[s_floor][s_x][s_y][new_x][new_y][t + 1] = new_w;
search(s_floor, s_x, s_y, new_x, new_y, t + 1);
}
}
}
vis[x][y] = false;
}
void find_way(int &s_floor, int &s_x, int &s_y, int &e_x, int &e_y, int &t_dist, int &t_weight, int x, int y, int dist){
vis[x][y] = true;
if (x == e_x && y == e_y && t_weight == max_w[s_floor][s_x][s_y][x][y][dist] && dist == t_dist){
found = true;
for (int i = 1; i <= dist / 2; i ++){
char tmp = out[size + i];
out[size + i] = out[size + dist - i];
out[size + dist - i] = tmp;
}
size += dist - 1;
}
if (found) return;
for (int i = 0; i < 4; i ++){
int new_x = x + dir_x[i], new_y = y + dir_y[i];
if (new_x >= 0 && new_x < 4 && new_y >= 0 && new_y < 4 && !vis[new_x][new_y]){
int new_w = max_w[s_floor][s_x][s_y][x][y][dist] + w[s_floor][new_x][new_y];
if (new_w > max_w[s_floor][s_x][s_y][new_x][new_y][dist + 1]){
max_w[s_floor][s_x][s_y][new_x][new_y][dist + 1] = new_w;
if (found) return;
out[size + dist] = dis_c[i];
find_way(s_floor, s_x, s_y, e_x, e_y, t_dist, t_weight, new_x, new_y, dist + 1);
}
}
}
vis[x][y] = false;
}
int main(void){
scanf("%d", &n);
memset(link, 0, sizeof(link));
memset(dp, 255, sizeof(dp));
memset(max_w, 255, sizeof(max_w));
int s_x, s_y;
for (int c = 1; c <= n; c ++){
memset(vis, 0, sizeof(vis));
for (int i = 0; i < 4; i ++){
for (int j = 0; j < 4; j ++){
scanf("%d", &w[c][i][j]);
}
}
for (int i = 0; i < 4; i ++){
for (int j = 0; j < 4; j ++){
scanf("%d", &link[c][i][j]);
}
}
for (int i = 0; i < 4; i ++){
for (int j = 0; j < 4; j ++){
memset(vis, false, sizeof(vis));
max_w[c][i][j][i][j][1] = w[c][i][j];
search(c, i, j, i, j, 1);
}
}
}
scanf("%d%d", &s_x, &s_y);
s_x --; s_y --;
dp[0][0][s_x][s_y] = 0;
link[0][s_x][s_y] = 1;
for (int c = 1; c <= n; c ++){
for (int i = 0; i < 4; i ++){
for (int j = 0; j < 4; j ++){
for (int k = 0; k <= c * 16; k ++){
if (dp[c - 1][k][i][j] >= 0 && link[c - 1][i][j] > 0){
for (int l = 0; l < 4; l ++){
for (int m = 0; m < 4; m ++){
for (int o = 1; o <= 16; o ++){
if (max_w[c][i][j][l][m][o] == -1) continue;
if (dp[c - 1][k][i][j] + max_w[c][i][j][l][m][o] > dp[c][k + o][l][m]){
dp[c][k + o][l][m] = dp[c - 1][k][i][j] + max_w[c][i][j][l][m][o];
last[c][k + o][l][m][0] = i;
last[c][k + o][l][m][1] = j;
last[c][k + o][l][m][2] = o;
last[c][k + o][l][m][3] = max_w[c][i][j][l][m][o];
}
}
}
}
}
}
}
}
}
lld ans = -1.0;
int ans_i, ans_j, ans_w;
for (int i = 0; i < 4; i ++){
for (int j = 0; j < 4; j ++){
for (int k = 1; k < n * 16; k ++){
if (lld(dp[n][k][i][j]) / lld(k) > ans){
ans = lld(dp[n][k][i][j]) / lld(k);
ans_i = i;
ans_j = j;
ans_w = k;
}
}
}
}
memset(max_w, 255, sizeof(max_w));
printf("%.4lf\n", double(ans));
for (int i = n; i >= 1; i --){
int final_x = last[i][ans_w][ans_i][ans_j][0];
int final_y = last[i][ans_w][ans_i][ans_j][1];
int final_w = last[i][ans_w][ans_i][ans_j][2];
int weight = last[i][ans_w][ans_i][ans_j][3];
memset(vis, false, sizeof(vis));
found = false;
max_w[i][final_x][final_y][final_x][final_y][1] = w[i][final_x][final_y];
find_way(i, final_x, final_y, ans_i, ans_j, final_w, weight, final_x, final_y, 1);
ans_i = final_x;
ans_j = final_y;
ans_w -= final_w;
out[++ size] = 'D';
}
printf("%d\n", size);
for (int i = size - 1; i >= 0; i --){
printf("%c", out[i]);
}
printf("\n");
system("pause");
}