不堪回首第一次区域赛(真哭JJ)
真心态爆炸一场(密码给错了十分钟 不能复制粘贴case linux用到哭 签到签不出来)
所以试了一下文件读入操作。。
题目真不是很难,就是建图稍微要思路清楚一点。。。
#include<bits/stdc++.h>
using namespace std;
#define FSCI(i,l,r,a) for(int i=l;i<=r;i++) scanf("%d",&a[i])
#define SCI(a) scanf("%d",&a)
#define SCS(a) scanf("%s",a)
#define LL long long
#define mod 1000000007
#define N 1005
/***************************************************************************************************************************************************/
char mp[N*5][N*7];
vector<int>v[N*N];
int r,c;
int S,E;
int ans=INT_MAX;
int dp[N*N];
//ifstream in("C:\\Users\\RSHS\\Desktop\\input case.txt");
struct no{
int index;
int step;
no(){}; //overwrite
no(int a,int b):index(a),step(b){} //overwrite
}node;
void readmp(){
cin>>r>>c;
getchar();
for(int i=0;i<4*r+3;i++)
cin.getline(mp[i],7000);
/*in>>r>>c;
in.getline(mp[0],7000); //瞎jier吸收回车
for(int i=0;i<4*r+3;i++){
in.getline(mp[i],7000);
}
for(int i=0;i<4*r+3;i++){
for(int j=0;j<strlen(mp[i]);j++)
cout<<mp[i][j];
cout<<endl;
}
cout<<mp[4][10];*/
}
void ST(int h,int l,int index){
if(mp[h][l]=='S') S=index;
if(mp[h][l]=='T') E=index;
}
void isarc1(int h,int l,int index){
if(mp[h-2][l]!='-') {
v[index].push_back(index-1);
}
if(mp[h+2][l]!='-') {
v[index].push_back(index+1);
}
if(mp[h-1][l-3]!='/'){
v[index].push_back(index-r-1);
}
if(mp[h+1][l+3]!='/'){
v[index].push_back(index+r);
}
if(mp[h-1][l+3]!='\\'){
v[index].push_back(index+r-1);
}
if(mp[h+1][l-3]!='\\'){
v[index].push_back(index-r);
}
}
void isarc2(int h,int l,int index){
if(mp[h-2][l]!='-') {
v[index].push_back(index-1);
}
if(mp[h+2][l]!='-') {
v[index].push_back(index+1);
}
if(mp[h-1][l-3]!='/'){
v[index].push_back(index-r);
}
if(mp[h+1][l+3]!='/'){
v[index].push_back(index+r+1);
}
if(mp[h-1][l+3]!='\\'){
v[index].push_back(index+r);
}
if(mp[h+1][l-3]!='\\'){
v[index].push_back(index-r+1);
}
}
void initv(){
for(int i=0;i<=r*c+1;i++)
v[i].clear();
int index=0;
for(int i=1;i<=c;i++){
for(int j=1;j<=r;j++){
if(i%2){
int l=6*(i-1)+5-1;
int h=4*(j-1)+3-1;
index++;
ST(h,l,index);
isarc1(h,l,index);
}
else{
int l=6*(i-1)+5-1;
int h=4*(j-1)+5-1;
index++;
ST(h,l,index);
isarc2(h,l,index);
}
}
}
}
void bfs(){
for(int i=0;i<=r*c+1;i++){
dp[i]=INT_MAX;
}
queue<no>q;
q.push({S,1});dp[S]=1;
while(!q.empty()){
int nowi=q.front().index ,nows=q.front().step;
if(nowi==E) {ans=min(ans,nows);}
q.pop();
for(int i=0;i<(int)v[nowi].size();i++){
if(nows+1<dp[v[nowi][i]]){
q.push({v[nowi][i],nows+1});
dp[v[nowi][i]]=nows+1;
}
}
}
}
int main(){
int T;//in>>T;
cin>>T;
while(T--){
ans=INT_MAX;
readmp();
initv();
bfs();
if(ans!=INT_MAX)
cout<<ans<<endl;
else {
cout<<-1<<endl;
}
}
return 0;
}
突然可以直接next数组在char数组上bfs。可以减少代码量,算了都一样。
想说些什么,还是不奶自己了。
菜就是菜。