Mirror Maze 镜面反射
题目描述
You are given a grid of R rows (numbered from 1 to R from north to south) and C columns (numbered from 1 to C from west to east). Every cell in this grid is a square of the same size. The cell located at row r and column c is denoted as (r, c) . Each cell can either be empty or have a mirror in one of the cell’s diagonals.
Each mirror is represented by a line segment. The mirror is type 1 if it is positioned diagonally from the southwest corner to the northeast corner of the cell, or type 2 for the other diagonal.
These mirrors follow the law of refl ection, that is, the angle of refl ection equals the angle of incidence.
Formally, for type 1 mirror, if a beam of light comes from the north, south, west, or east of the cell, then it will be refl ected to the west, east, north, and south of the cell, respectively. Similarly, for type 2 mirror, if a beam of light comes from the north, south, west, or east of the cell, then it will be refl ected to the east, west,south, and north of the cell, respectively.
You want to put a laser from outside the grid such that all mirrors are hit by the laser beam. There are 2 · (R + C) possible locations to put the laser:
• from the north side of the grid at column c , for 1 ≤ c ≤ C , shooting a laser beam to the south;
• from the south side of the grid at column c , for 1 ≤ c ≤ C , shooting a laser beam to the north;
• from the east side of the grid at row r , for 1 ≤ r ≤ R , shooting a laser beam to the west; and
• from the west side of the grid at row r , for 1 ≤ r ≤ R , shooting a laser beam to the east.Determine all possible locations for the laser such that all mirrors are hit by the laser beam.
输入
The first line consists of two integers R C ( 1 ≤ R, C ≤ 200 ).
Each of the next R lines consists of a string S r of length C . The cth character of string S r represents cell (r, c) . Each character can either be . if the cell is empty, / if the cell has type 1 mirror, or \ if the cell has type 2 mirror. There is at least one mirror in the grid.输出
Output a single integer representing the number of possible locations for the laser such that all mirrors are hit by the laser beam. Denote this number as k .
If k > 0 , then output k space-separated strings representing the location of the laser. Each string consists of a character followed without any space by an integer. The character represents the side of the grid, which could be N , S , E , or W if you put the laser on the north, south, east, or west side of the grid,
respectively. The integer represents the row/column number. You can output the strings in any order.样例输入
复制
【样例1】 4 4 .//. .\\. .\/. .... 【样例2】 4 6 ./..\. .\...\ ./../\ ...... 【样例3】 4 4 .... ./\. .\/. ....
样例输出
复制
【样例1】 2 N3 W2 【样例2】 2 E3 S2 【样例3】 0
提示
Explanation for the sample input/output #1
The following illustration shows one of the solutions of this sample.
Explanation for the sample input/output #2
The following illustration shows one of the solutions of this sample.
ans保存的时候不能用 ' 1 ' + i 保存。。。。这个只适用于个位数
代码
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;int r,c,cnt[209][209];
int fx[]{-1,0,1,0},fy[]{0,1,0,-1};
char mirror[209][209];int refl(int x,int y,int p)
{if(mirror[x][y]=='/'){if(p==0)return 1;if(p==1)return 0;if(p==2)return 3;return 2;}if(p==0)return 3;if(p==1)return 2;if(p==2)return 1;return 0;
}int check()
{for(int i=0;i<r;++i){for(int j=0;j<c;++j){if(mirror[i][j]!='.'&&!cnt[i][j])return 0;}}return 1;
}void go(int x,int y,int p)
{ memset(cnt,0,sizeof(cnt));while(true){if(x<0||x>=r||y<0||y>=c)return; if(mirror[x][y]!='.')cnt[x][y]=1,p=refl(x,y,p);x+=fx[p],y+=fy[p];}
}int main()
{cin>>r>>c;for(int i=0;i<r;++i){for(int j=0;j<c;++j)cin>>mirror[i][j];}vector<string>ans;for(int i=0;i<c;++i){go(0,i,2);if(check())ans.push_back("N"+to_string(1+i));}for(int i=0;i<c;++i){go(r-1,i,0);if(check())ans.push_back("S"+to_string(1+i));}for(int i=0;i<r;++i){go(i,0,1);if(check())ans.push_back("W"+to_string(1+i));}for(int i=0;i<r;++i){go(i,c-1,3);if(check())ans.push_back("E"+to_string(1+i));}cout<<ans.size()<<'\n';if(!ans.empty()){for(auto i:ans)cout<<i<<" ";}return 0;
}