本文主要是介绍【hdu1241Oil Deposits】【HDU 2612 Find a way】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
HDU1241Oil Deposits
求联通块数量
Sample Input
1 1
*
3 5
*@*@*
**@**
*@*@*
1 8
@@****@*
5 5
****@
*@@*@
*@**@
@@@*@
@@**@
0 0
Sample Output
0
1
2
2
DFS:
#include <iostream>
#include<cstdio>
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
typedef long long ll;
int n,m;
char map[110][110];
int dir[10][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};
struct node{int x,y;//node(int xx,int yy){x=xx,y=yy;}
};
void dfs(int x,int y){for(int i=0;i<8;++i){int dx=x+dir[i][0];int dy=y+dir[i][1];if(dx<1||dx>n||dy<1||dy>m)continue;if(map[dx][dy]=='@'){map[dx][dy]='*';dfs(dx,dy);}}
}
int main(){while(scanf("%d %d",&n,&m)!=EOF){if(n==0||m==0)break;for(int i=1;i<=n;++i)scanf("%s",map[i]+1);int ans=0;for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){if(map[i][j]=='@'){map[i][j]='*';ans++;dfs(i,j);}}printf("%d\n",ans);}return 0;
}
bfs:
#include <iostream>#include<cstdio>#include <iostream>#include <cstring>#include <queue>using namespace std;typedef long long ll;int n,m;char map[110][110];int dir[10][2]={{1,0},{-1,0},{0,1},{0,-1},{1,1},{-1,1},{1,-1},{-1,-1}};struct node{int x,y;//node(int xx,int yy){x=xx,y=yy;}};void bfs(int x,int y){queue<node> q;node point;point.x=x; point.y=y;q.push(point);while(!q.empty()){//cout<<1<<endl;node cur=q.front();q.pop();map[cur.x][cur.y]='*';node tmp;cout<<cur.x<<' '<<cur.y<<' '<<map[cur.x][cur.y]<<endl;for(int i=0;i<8;++i){int dx=cur.x+dir[i][0];int dy=cur.y+dir[i][1];if(dx<1||dx>n||dy<1||dy>m)continue;if(map[dx][dy]=='@'){map[dx][dy]='*';tmp.x=dx;tmp.y=dy;q.push(tmp);}}}}int main(){while(scanf("%d %d",&n,&m)!=EOF){if(n==0||m==0)break;for(int i=1;i<=n;++i)scanf("%s",map[i]+1);int ans=0;for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){if(map[i][j]=='@'){map[i][j]='*';ans++;bfs(i,j);}}printf("%d\n",ans);}return 0;}
HDU 2 612 Find a way
题意就是在图中找一条最短路径使得 Y M到一家kfc(@)约会,求出约会最短的路径
@可能有多家,#是障碍物
各种Exceeded得姿势,先是用的pair导致得是TLE,改了之后又MLE,之后又回到TLE,
最终俩个TLE一个是pair还有一个是vis标记得时候在进度队列的时候进行标记得话,相对来说会减少很多次进队次数
#include <cstdio>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
const int inf=99999;
int n,m;
char mp[201][201];
int step[201][201][2];
int vis[201][201];
int dir[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
int flag;
struct node{
int x,y;};
void bfs(int x,int y){queue<node >q;node p,tmp;//pair<int,int> p;p.x=x,p.y=y;q.push(p);while(!q.empty()){p=q.front();q.pop();//int x=p.x;//int y=p.y;//vis[p.x][p.y]=1;for(int i=0;i<4;++i){tmp.x=p.x+dir[i][0];tmp.y=p.y+dir[i][1];if(tmp.x<0||tmp.x>=n||tmp.y<0||tmp.y>=m||vis[tmp.x][tmp.y]||mp[tmp.x][tmp.y]=='#')continue;step[tmp.x][tmp.y][flag]=step[p.x][p.y][flag]+1;q.push(tmp);vis[tmp.x][tmp.y]=1;}}
}
int main(){while(scanf("%d%d",&n,&m)!=EOF){for(int i=0;i<n;++i)scanf("%s",mp[i]);memset(step, inf,sizeof(step));for(int i=0;i<n;++i)for(int j=0;j<m;++j){if(mp[i][j]=='Y'){memset(vis,0,sizeof(vis));flag=0;step[i][j][flag]=0;bfs(i,j);}if(mp[i][j]=='M'){memset(vis,0,sizeof(vis));flag=1;step[i][j][flag]=0;bfs(i,j);}}int dis=inf;for(int i=0;i<n;++i)for(int j=0;j<m;++j){if(mp[i][j]=='@'&&dis>step[i][j][0]+step[i][j][1]){dis=step[i][j][0]+step[i][j][1];//dis=min(dis,step[i][j][0]+step[i][j][1]);}}printf("%d\n",dis*11);}return 0;
}
//MLE&&TLE:
#include <cstdio>
#include <iostream>
#include <queue>
#include <bits/stdc++.h>
#include <cstring>
using namespace std;
const int inf=0x3f3f3f3f;
int n,m;
char mp[220][220];
int step[220][220][2];
int vis[220][220];
int dir[4][2]={{1,0},{0,1},{0,-1},{-1,0}};
int flag;
void bfs(int x,int y){queue<pair<int,int> >q;pair<int,int> p;q.push(make_pair(x,y));while(!q.empty()){p=q.front();q.pop();int x=p.first;int y=p.second;for(int i=0;i<4;++i){int dx=x+dir[i][0];int dy=y+dir[i][1];if(dx<1||dx>n||dy<1||dy>m||vis[dx][dy]||mp[dx][dy]=='#')continue;step[dx][dy][flag]=step[x][y][flag]+1;q.push(make_pair(dx,dy));vis[x][y]=1;}}
}
int main(){while(scanf("%d%d",&n,&m)!=EOF){for(int i=1;i<=n;++i)scanf("%s",mp[i]+1);memset(step, inf,sizeof(step));for(int i=1;i<=n;++i)for(int j=1;j<=m;++j)step[i][j][0]=step[i][j][1]=inf;for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){if(mp[i][j]=='Y'){memset(vis,0,sizeof(vis));flag=0;step[i][j][flag]=0;bfs(i,j);}if(mp[i][j]=='M'){memset(vis,0,sizeof(vis));flag=1;step[i][j][flag]=0;bfs(i,j);}}int dis=inf;for(int i=1;i<=n;++i)for(int j=1;j<=m;++j){if(mp[i][j]=='@'){dis=min(dis,step[i][j][0]+step[i][j][1]);}}printf("%d\n",dis*11);}return 0;
}
这篇关于【hdu1241Oil Deposits】【HDU 2612 Find a way】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!