1、
http://acm.hdu.edu.cn/showproblem.php?pid=3345
2、题目大意:
给定一张图,其中'R'代表河,从此方格经过消费3个mv,'T'代表树,从此格经过消费2个mv,'.'代表正常的方格,从次经过消费一个mv,‘E'代表敌人,不能从此格经过,而且此格的相邻的格子会使得mv减成0,’P‘代表朋友,不能停留在此方格,但是可以从此次方格经过,消费一个mv,Y代表现在的起始位置,问从起始位置用当前拥有的mv,可以走哪些格,可以走的格子用*表示
3、题目很简单,从当前的格子搜即可,不过有陷阱,就是同一个格子可以重复走,当前格子的mv需要不断更新到最大值,就这个错了10多遍,再有就是输入时注意,如果是C语言,需要有getchar,C++输入直接用就行
4、题目:
War Chess
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1514 Accepted Submission(s): 357
Problem Description
War chess is hh's favorite game:
In this game, there is an N * M battle map, and every player has his own Moving Val (MV). In each round, every player can move in four directions as long as he has enough MV. To simplify the problem, you are given your position and asked to output which grids you can arrive.
In the map:
'Y' is your current position (there is one and only one Y in the given map).
'.' is a normal grid. It costs you 1 MV to enter in this gird.
'T' is a tree. It costs you 2 MV to enter in this gird.
'R' is a river. It costs you 3 MV to enter in this gird.
'#' is an obstacle. You can never enter in this gird.
'E's are your enemies. You cannot move across your enemy, because once you enter the grids which are adjacent with 'E', you will lose all your MV. Here “adjacent” means two grids share a common edge.
'P's are your partners. You can move across your partner, but you cannot stay in the same grid with him final, because there can only be one person in one grid.You can assume the Ps must stand on '.' . so ,it also costs you 1 MV to enter this grid.
In this game, there is an N * M battle map, and every player has his own Moving Val (MV). In each round, every player can move in four directions as long as he has enough MV. To simplify the problem, you are given your position and asked to output which grids you can arrive.
In the map:
'Y' is your current position (there is one and only one Y in the given map).
'.' is a normal grid. It costs you 1 MV to enter in this gird.
'T' is a tree. It costs you 2 MV to enter in this gird.
'R' is a river. It costs you 3 MV to enter in this gird.
'#' is an obstacle. You can never enter in this gird.
'E's are your enemies. You cannot move across your enemy, because once you enter the grids which are adjacent with 'E', you will lose all your MV. Here “adjacent” means two grids share a common edge.
'P's are your partners. You can move across your partner, but you cannot stay in the same grid with him final, because there can only be one person in one grid.You can assume the Ps must stand on '.' . so ,it also costs you 1 MV to enter this grid.
Input
The first line of the inputs is T, which stands for the number of test cases you need to solve.
Then T cases follow:
Each test case starts with a line contains three numbers N,M and MV (2<= N , M <=100,0<=MV<= 65536) which indicate the size of the map and Y's MV.Then a N*M two-dimensional array follows, which describe the whole map.
Then T cases follow:
Each test case starts with a line contains three numbers N,M and MV (2<= N , M <=100,0<=MV<= 65536) which indicate the size of the map and Y's MV.Then a N*M two-dimensional array follows, which describe the whole map.
Output
Output the N*M map, using '*'s to replace all the grids 'Y' can arrive (except the 'Y' grid itself). Output a blank line after each case.
Sample Input
5 3 3 100 ... .E. ..Y5 6 4 ...... ....PR ..E.PY ...ETT ....TT2 2 100 .E EY5 5 2 ..... ..P.. .PYP. ..P.. .....3 3 1 .E. EYE ...
Sample Output
... .E* .*Y...*** ..**P* ..E*PY ...E** ....T*.E EY..*.. .*P*. *PYP* .*P*. ..*...E. EYE .*.
5、ac代码,自己第一遍写的有些麻烦
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char map[105][105];
int sx,sy,n,m;
int visit[105][105];
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
struct node
{int x;int y;int cost;
} a[10015];
void bfs(int mv)
{memset(visit,-1,sizeof(visit));a[0].x=sx;a[0].y=sy;a[0].cost=mv;visit[sx][sy]=mv;int start=0,end=1;node cur,change;while(start<end){cur=a[start++];if(cur.cost==0)continue;for(int i=0; i<4; i++){int tx=cur.x+dir[i][0];int ty=cur.y+dir[i][1];if(map[tx][ty]=='#' ||map[tx][ty]=='E' || map[tx][ty]=='Y' )continue;else if(tx>=0 && tx<n && ty>=0 && ty<m && cur.cost>=1 ){change.x=tx;change.y=ty;if(ty+1<m && map[tx][ty+1]=='E'){if((map[tx][ty]=='.' && cur.cost>=1) || (map[tx][ty]=='R' && cur.cost>=3) || (map[tx][ty]=='T' && cur.cost>=2) || (map[tx][ty]=='P' && cur.cost>=1)){visit[tx][ty]=0;if(map[tx][ty]!='P')map[tx][ty]='*';}}if(ty-1>=0 && map[tx][ty-1]=='E'){if((map[tx][ty]=='.' && cur.cost>=1) || (map[tx][ty]=='R' && cur.cost>=3) || (map[tx][ty]=='T' && cur.cost>=2) || (map[tx][ty]=='P' && cur.cost>=2)){if(map[tx][ty]!='P')map[tx][ty]='*';visit[tx][ty]=0;}}if(tx+1<n && map[tx+1][ty]=='E'){if((map[tx][ty]=='.' && cur.cost>=1) || (map[tx][ty]=='R' && cur.cost>=3) || (map[tx][ty]=='T' && cur.cost>=2) || (map[tx][ty]=='P' && cur.cost>=2)){if(map[tx][ty]!='P')map[tx][ty]='*';visit[tx][ty]=0;}}if(tx-1>=0 && map[tx-1][ty]=='E'){if((map[tx][ty]=='.' && cur.cost>=1) || (map[tx][ty]=='R' && cur.cost>=3) || (map[tx][ty]=='T' && cur.cost>=2) || (map[tx][ty]=='P' && cur.cost>=2)){if(map[tx][ty]!='P')map[tx][ty]='*';visit[tx][ty]=0;}}if(map[tx][ty]=='.' && cur.cost>=1){change.cost=cur.cost-1;if(change.cost>visit[tx][ty]){a[end++]=change;visit[tx][ty]=change.cost;map[tx][ty]='*';}}else if(map[tx][ty]=='R' && cur.cost>=3){change.cost=cur.cost-3;if(change.cost>visit[tx][ty]){a[end++]=change;visit[tx][ty]=change.cost;map[tx][ty]='*';}}else if(map[tx][ty]=='T' && cur.cost>=2){change.cost=cur.cost-2;if(change.cost>visit[tx][ty]){a[end++]=change;visit[tx][ty]=change.cost;map[tx][ty]='*';}}else if(map[tx][ty]=='P' && cur.cost>=1){change.cost=cur.cost-1;if(change.cost>visit[tx][ty]){a[end++]=change;visit[tx][ty]=change.cost;}}}}}
}
int main()
{int t,mv;cin>>t;while(t--){cin>>n>>m>>mv;for(int i=0; i<n; i++){for(int j=0; j<m; j++){cin>>map[i][j];if(map[i][j]=='Y'){sx=i;sy=j;}}}bfs(mv);for(int i=0; i<n; i++){for(int j=0; j<m; j++){printf("%c",map[i][j]);}printf("\n");}printf("\n");}return 0;
}
看网上代码,将自己代码优化了一下
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
char map[105][105];
int sx,sy,n,m;
int visit[105][105];//注意一个点的mv值可能有多个,错了10多遍
int dir[4][2]= {1,0,0,1,-1,0,0,-1};
struct node
{int x;int y;int cost;
} a[1000015];//数组开大,否则越界
int mvCost(int tx,int ty,int cost)
{if(map[tx][ty]=='R')cost-=3;else if(map[tx][ty]=='T')cost-=2;elsecost-=1;int flag=0;for(int i=0;i<4;i++){int ttx=tx+dir[i][0];int tty=ty+dir[i][1];if(ttx>=0 && ttx<n && tty>=0 && tty<m){if(map[ttx][tty]=='E'){flag=1;break;}}}if(flag==1 && cost>0)cost=0;return cost;}
void bfs(int mv)
{memset(visit,-1,sizeof(visit));a[0].x=sx;a[0].y=sy;a[0].cost=mv;visit[sx][sy]=mv;int start=0,end=1;node cur,change;while(start<end){cur=a[start++];if(cur.cost==0)continue;for(int i=0; i<4; i++){int tx=cur.x+dir[i][0];int ty=cur.y+dir[i][1];if(map[tx][ty]=='#' ||map[tx][ty]=='E' || map[tx][ty]=='Y' )continue;else if(tx>=0 && tx<n && ty>=0 && ty<m ){int Cost=mvCost(tx,ty,cur.cost);if(Cost<0)continue;else{if(Cost>visit[tx][ty]){change.x=tx;change.y=ty;change.cost=Cost;a[end++]=change;visit[tx][ty]=Cost;if(map[tx][ty]!='P')map[tx][ty]='*';}}}}}
}
int main()
{int t,mv;cin>>t;while(t--){cin>>n>>m>>mv;for(int i=0; i<n; i++){for(int j=0; j<m; j++){cin>>map[i][j];if(map[i][j]=='Y'){sx=i;sy=j;}}}bfs(mv);for(int i=0; i<n; i++){for(int j=0; j<m; j++){printf("%c",map[i][j]);}printf("\n");}printf("\n");}return 0;
}