Problem J: Willful
Time Limit: 2 Sec Memory Limit: 128 MBTopic description
The willful boss's favorite game is Fighting the Landlord, but today he wants to try a tank battle. In the game, he can use his tank to launch a laser to attack the enemy, and the laser can be reflected by a mirror.
The wayward boss now wants to calculate how many enemies the laser can kill in total.
Note: The laser passes through the enemy after killing the enemy; the laser passes directly when it encounters its own tank; the laser can choose to fire horizontally or vertically.
enter
Multiple sets of input and output
Enter two integers n, m (1 ≤ n, m ≤ 1000) in the first line, which represent the number of rows and columns of the map, respectively, and the following n lines describe each line of the map.
. Indicates open space
* Represents an obstacle (the light cannot pass through, and the propagation ends when the light hits the obstacle)
T The initial position of the wayward boss's tank
E for enemy
/ Indicates mirrors placed from bottom left to top right
\ Indicates mirrors placed from top left to bottom right
output
An integer representing the maximum number of enemies that can be killed
sample input
5 5 .*/E\ E*.*. E*TEE \.../ . * \ EE
Sample output
4
Analysis: Enumerate the four directions of up, down, left, and right, and take the one that can kill the most enemies. Use vis[i][j][k] to indicate whether to pass the point (i, j) in the k direction to avoid falling into an infinite loop.
The XOR operation can be used to realize the replacement of numbers within a certain range, reducing the judgment of if.
#include<bits/stdc++.h> using namespace std; char a[1100][1100]; int n,m; const int dx[]= {-1,0,1,0}; const int dy[]= {0,1,0,-1}; int vis[1100][1100][4]; bool ok(int x,int y) { for(int i=0; i<4; ++i) if(vis[x][y][i])return false; return true; } intmain () { //freopen("i.txt","r",stdin); while(scanf("%d%d",&m,&n)==2) { int xx,yy; for(int i=0; i<m; ++i) for(int j=0; j<n; ++j) { scanf(" %c",&a[i][j]); if(a[i][j]=='T') xx=i,yy=j; } int maxn=0; for(int i=0; i<4; ++i) { memset(vis,0,sizeof vis); int p=i; int x=xx,y=yy; int cnt=0; while(x>=0&&x<m&&y>=0&&y<n&&!vis[x][y][p]&&a[x][y]!='*') { if(a[x][y]=='E'&&ok(x,y)) ++cnt; else if(a[x][y]=='/') p^=1; else if(a[x][y]=='\\') p^=3; force[x][y][p ] = 1 x+=dx[p],y+=dy[p]; } maxn=max(maxn,cnt); } printf("%d\n",maxn); } }