-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
405 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"files.associations": { | ||
"random": "c", | ||
"limits": "c" | ||
} | ||
} |
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
#include <stdio.h> | ||
int main() | ||
{ | ||
printf("%d",sizeof(int*)); | ||
char s4[2][]={"abc","xyz"}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
/* | ||
(x-1,y-1) (x-1,y) (x-1,y+1) | ||
(x,y-1) (x,y) (x,y+1) | ||
(x+1,y-1) (x+1,y) (x+1,y+1) | ||
*/ | ||
#include <stdio.h> | ||
void print(int a[120][120]); | ||
void map(int a[120][120], int x, int y, int *coin); | ||
int min = 10000; | ||
int magicflag = 1; // 1为可以使用魔法 | ||
int m, n; | ||
#define find(x, y) (a[x][y] < 2) && (x > 0) && (y > 0) && (x <= m) && (y <= m) | ||
#define find2(x, y) (a[x][y] == 2) && (x > 0) && (y > 0) && (x <= m) && (y <= m) && (magicflag == 1) | ||
|
||
int main() { | ||
int a[120][120]; | ||
scanf("%d %d", &m, &n); | ||
getchar(); | ||
for (int i = 1; i <= m; i++) { | ||
for (int j = 1; j <= m; j++) { | ||
a[i][j] = 2; | ||
} | ||
} | ||
for (int i = 0; i < n; i++) { | ||
int k, j, l; | ||
scanf("%d %d %d", &k, &j, &l); | ||
for (int p = 0; p <= l; p++) { | ||
a[k][j] = l; | ||
} | ||
getchar(); | ||
} | ||
// for (int i = 1; i <= m; i++) { | ||
// for (int j = 1; j <= m; j++) { | ||
// printf("%d ", a[i][j]); | ||
// } | ||
// printf("\n"); | ||
// } | ||
// printf("\n"); | ||
int coin = 0; | ||
map(a, 1, 1, &coin); | ||
if (min == 10000) | ||
printf("-1"); | ||
else | ||
printf("%d", min); | ||
} | ||
|
||
void map(int a[120][120], int x, int y, int *coin) { | ||
//print(a); | ||
//printf("coin=%d\n", *coin); | ||
if ((x == m) && (y == m)) { | ||
min = ((*coin) < min) ? (*coin) : min; | ||
// print(a); | ||
// printf("final_coin=%d\n", *coin); | ||
return; | ||
} | ||
if (find(x - 1, y)) { | ||
int temp = magicflag; | ||
magicflag = 1; | ||
if (a[x - 1][y] == a[x][y]) { | ||
a[x][y] += 10; | ||
*coin += 0; | ||
map(a, x - 1, y, coin); | ||
magicflag = temp; | ||
a[x][y] -= 10; | ||
} else { | ||
a[x][y] += 10; | ||
*coin += 1; | ||
map(a, x - 1, y, coin); | ||
magicflag = temp; | ||
a[x][y] -= 10; | ||
*coin -= 1; | ||
} | ||
} else if (find2(x - 1, y)) { | ||
magicflag = 0; | ||
a[x - 1][y] = a[x][y]; | ||
a[x][y] += 10; | ||
*coin += 2; | ||
map(a, x - 1, y, coin); | ||
a[x][y] -= 10; | ||
magicflag = 1; | ||
*coin -= 2; | ||
a[x - 1][y] = 2; | ||
} | ||
if (find(x, y - 1)) { | ||
int temp = magicflag; | ||
magicflag = 1; | ||
if (a[x][y - 1] == a[x][y]) { | ||
a[x][y] += 10; | ||
*coin += 0; | ||
map(a, x, y - 1, coin); | ||
magicflag = temp; | ||
a[x][y] -= 10; | ||
} else { | ||
a[x][y] += 10; | ||
*coin += 1; | ||
map(a, x, y - 1, coin); | ||
magicflag = temp; | ||
a[x][y] -= 10; | ||
*coin -= 1; | ||
} | ||
} else if (find2(x, y - 1)) { | ||
magicflag = 0; | ||
a[x][y - 1] = a[x][y]; | ||
a[x][y] += 10; | ||
*coin += 2; | ||
map(a, x, y - 1, coin); | ||
a[x][y] -= 10; | ||
magicflag = 1; | ||
*coin -= 2; | ||
a[x][y - 1] = 2; | ||
} | ||
if (find(x, y + 1)) { | ||
int temp = magicflag; | ||
magicflag = 1; | ||
if (a[x][y + 1] == a[x][y]) { | ||
a[x][y] += 10; | ||
*coin += 0; | ||
map(a, x, y + 1, coin); | ||
magicflag = temp; | ||
a[x][y] -= 10; | ||
} else { | ||
a[x][y] += 10; | ||
*coin += 1; | ||
map(a, x, y + 1, coin); | ||
magicflag = temp; | ||
a[x][y] -= 10; | ||
*coin -= 1; | ||
} | ||
} else if (find2(x, y + 1)) { | ||
magicflag = 0; | ||
a[x][y + 1] = a[x][y]; | ||
a[x][y] += 10; | ||
*coin += 2; | ||
map(a, x, y + 1, coin); | ||
a[x][y] -= 10; | ||
magicflag = 1; | ||
*coin -= 2; | ||
a[x][y + 1] = 2; | ||
} | ||
if (find(x + 1, y)) { | ||
int temp = magicflag; | ||
magicflag = 1; | ||
if (a[x + 1][y] == a[x][y]) { | ||
a[x][y] += 10; | ||
*coin += 0; | ||
map(a, x + 1, y, coin); | ||
magicflag = temp; | ||
a[x][y] -= 10; | ||
} else { | ||
a[x][y] += 10; | ||
*coin += 1; | ||
map(a, x + 1, y, coin); | ||
magicflag = temp; | ||
a[x][y] -= 10; | ||
*coin -= 1; | ||
} | ||
} else if (find2(x + 1, y)) { | ||
magicflag = 0; | ||
a[x + 1][y] = a[x][y]; | ||
a[x][y] += 10; | ||
*coin += 2; | ||
map(a, x + 1, y, coin); | ||
a[x][y] -= 10; | ||
magicflag = 1; | ||
*coin -= 2; | ||
a[x + 1][y] = 2; | ||
} | ||
} | ||
|
||
// void print(int a[120][120]) { | ||
// for (int i = 1; i <= m; i++) { | ||
// for (int j = 1; j <= m; j++) { | ||
// printf("%d ", a[i][j]); | ||
// } | ||
// printf("\n"); | ||
// } | ||
// printf("\n"); | ||
// } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include <stdio.h> | ||
#include <limits.h> | ||
|
||
void map(int x, int y, int coin, int magicflag); | ||
int min = INT_MAX; | ||
int m, n; | ||
int a[120][120]; | ||
int dp[120][120][2]; // 动态规划数组,记录每个状态的最小金币数 | ||
|
||
#define find(x, y) (a[x][y] < 2) && (x > 0) && (y > 0) && (x <= m) && (y <= m) | ||
#define find2(x, y) (a[x][y] == 2) && (x > 0) && (y > 0) && (x <= m) && (y <= m) && (magicflag == 1) | ||
|
||
int main() { | ||
scanf("%d %d", &m, &n); | ||
getchar(); | ||
for (int i = 1; i <= m; i++) { | ||
for (int j = 1; j <= m; j++) { | ||
a[i][j] = 2; | ||
dp[i][j][0] = dp[i][j][1] = INT_MAX; // 初始化动态规划数组 | ||
} | ||
} | ||
for (int i = 0; i < n; i++) { | ||
int k, j, l; | ||
scanf("%d %d %d", &k, &j, &l); | ||
for (int p = 0; p <= l; p++) { | ||
a[k][j] = l; | ||
} | ||
getchar(); | ||
} | ||
map(1, 1, 0, 1); | ||
if (min == INT_MAX) | ||
printf("-1"); | ||
else | ||
printf("%d", min); | ||
} | ||
|
||
void map(int x, int y, int coin, int magicflag) { | ||
if (coin >= min) return; // 剪枝优化 | ||
if (coin >= dp[x][y][magicflag]) return; // 动态规划剪枝 | ||
dp[x][y][magicflag] = coin; | ||
|
||
if ((x == m) && (y == m)) { | ||
min = (coin < min) ? coin : min; | ||
return; | ||
} | ||
|
||
int directions[4][2] = {{1, 0}, {0, 1}, {0, -1}, {-1, 0}}; | ||
for (int i = 0; i < 4; i++) { | ||
int nx = x + directions[i][0]; | ||
int ny = y + directions[i][1]; | ||
|
||
if (find(nx, ny)) { | ||
if (a[nx][ny] == a[x][y]) { | ||
a[x][y] += 10; | ||
map(nx, ny, coin, 1); | ||
a[x][y] -= 10; | ||
} else { | ||
a[x][y] += 10; | ||
map(nx, ny, coin + 1, 1); | ||
a[x][y] -= 10; | ||
} | ||
} else if (find2(nx, ny)) { | ||
int temp = a[nx][ny]; | ||
a[nx][ny] = a[x][y]; | ||
a[x][y] += 10; | ||
map(nx, ny, coin + 2, 0); | ||
a[x][y] -= 10; | ||
a[nx][ny] = temp; | ||
} | ||
} | ||
} |
Oops, something went wrong.