题目
题解
–一道纯考代码实现的模拟题,2个小时起步(QAQ)
这道题可以分为三个部分:
1. 找到水箱和对应编号(可以通过从数字开始向外找到一个框)
2. 按水管高低,依次找到子水箱(从该水箱底部开始,一层层向上,遇到水管就沿着他一直走到子水箱)
3. dfs(只需要发现对于每个水箱里的水,肯定先会流向水管最低的那个子水箱,dfs就好)
不过注意,这水箱有2000多个,数组开小了就凉凉(坑死人啊)
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int MAXN=1005;
int n,m;
char g[MAXN][MAXN];
int sum;
struct hehe{
int x;
int y;
}num[MAXN*MAXN];
vector<int>sg[MAXN*10];
int hehe(int x,int y){
int shu=0;
while(g[x][y]>='0'&&g[x][y]<='9'){
shu*=10;
shu+=g[x][y]-'0';
y++;
}
return shu;
}
void liu(int x,int y,int u){
int fx=1;
while(!(g[x][y]=='-'&&fx==2)){
g[x][y]='.';
if(fx==1){
if(g[x][y+1]=='-')
y++;
else if(g[x][y-1]=='-')
y--;
else if(g[x][y+1]=='+'){
y++;
fx=2;
}
else if(g[x][y-1]=='+'){
y--;
fx=2;
}
}
else{
x++;
if(g[x][y]=='+')
fx=1;
}
}
while(g[x][y]!='+')
y--;
x++;
bool flag=0;
while(1){
if(flag)
break;
int y1=y+1;
while(g[x][y1]!='|'){
if(g[x][y1]>='0'&&g[x][y1]<='9'){
int shu=hehe(x,y1);
sg[u].push_back(shu);
flag=1;
break;
}
y1++;
}
x++;
}
}
void lb(int t){
int x=num[t].x;
int y=num[t].y;
int yl=y,yr=y;
if(g[x][y]=='.')
return ;
int shu=hehe(x,y);
while(g[x][yl]!='-')
x++;
while(g[x][yl]!='+')
yl--;
while(g[x][yr]!='+')
yr++;
x--;
while(g[x][yl]!='+'){
if(g[x][yl-1]=='-')
liu(x,yl-1,shu);
else if(g[x][yr+1]=='-')
liu(x,yr+1,shu);
else
x--;
}
}
void dfs(int x){
for(int i=0;i<sg[x].size();i++)
dfs(sg[x][i]);
printf("%d\n",x);
}
int main(){
freopen("clickbait.in","r",stdin);
freopen("clickbait.out","w",stdout);
cin>>n>>m;
for(int i=1;i<=n;i++){
for(int j=1;j<=m;j++){
cin>>g[i][j];
if(g[i][j]>='0'&&g[i][j]<='9'){
sum++;
num[sum].x=i;
num[sum].y=j;
}
}
}
for(int i=1;i<=sum;i++)
lb(i);
dfs(1);
return 0;
}