ACM学习笔记DAY5
- 今天学习的主要内容为BFS(广搜),那么这个算法一般用来求最小的路径。首先我展示一下BFS的实现过程。
那么下面我给出两道例题来更清楚的了解BFS
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxx=205;
int n,m,dis[maxx][maxx],vis[maxx][maxx],loc[4][4]={{1,0},{0,1},{-1,0},{0,-1}};
char s[maxx][maxx];
vector<int> ans;
void bfs(int a,int b)
{
queue<pair<int,int> > q;
q.push(make_pair(a,b));
vis[a][b]=1;
while(!q.empty())
{
int r=q.front().first,l=q.front().second;
q.pop();
for(int i=0;i<4;i++)
{
int u=r+loc[i][0],v=l+loc[i][1];
if(s[u][v]!='#'&&!vis[u][v]&&u<=n&&u>0&&v<=m&&v>0)
{
if(s[r][l]=='x') dis[u][v]++;
dis[u][v]+=dis[r][l]+1,vis[u][v]=1,q.push(make_pair(u,v));
if(s[u][v]=='r') ans.push_back(dis[u][v]);
}
}
}
}
int main()
{
int sl,sr;
while(cin>>n>>m)
{
memset(dis,0,sizeof dis);
memset(vis,0,sizeof vis);
ans.clear();
for(int i=1;i<=n;i++)
{
cin>>(s[i]+1);
for(int j=1;j<=m;j++)
{
if(s[i][j]=='a') sl=i,sr=j;
}
}
bfs(sl,sr);
if(ans.empty()) cout<<"Poor ANGEL has to stay in the prison all his life."<<endl;
else cout<<*min_element(ans.begin(),ans.end())<<endl;
}
}
#include <queue>
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
const int maxx=205;
int n,m,vis[maxx][maxx];
char s[maxx][maxx];
void bfs(int ss,int sd)
{
queue<pair<int,int>> q;
q.push(make_pair(ss,sd));
vis[ss][sd]=1;
while(!q.empty())
{
for(int i=-1;i<=1;i++)
{
for(int j=-1;j<=1;j++)
{
int l=q.front().first+i,r=q.front().second+j;
if(l<=n&&l>0&&r<=m&&r>0&&s[l][r]=='@'&&!vis[l][r])
{
vis[l][r]=1;
q.push(make_pair(l,r));
}
}
}
q.pop();
}
}
int main()
{
ios::sync_with_stdio(0);
while(cin>>n>>m&&n&&m)
{
int ans=0;
memset(vis,0,sizeof vis);
for(int i=1;i<=n;i++)
{
cin>>(s[i]+1);
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
if(s[i][j]=='@'&&!vis[i][j]) bfs(i,j),ans++;
}
}
cout<<ans<<endl;
}
}
接下来是今天没有ac的题目
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
struct node{
int x,y,t;
};
int n,m;
int mx[]={0,1,0,-1};
int my[]={1,0,-1,0};//右,下,左,上
char map[30][30];
int d[30][30];
int tx,ty,sx,sy;
node ft;
int judge(int x,int y){
if(x<0||x>=n||y<0||y>=m){
return 0;
}
if(d[x][y]){
return 0;
}
if(map[x][y]=='*'){
return 0;
}
return 1;
}
void bfs(){
queue<node> q;
ft.x=sx;
ft.y=sy;
ft.t=0;
q.push(ft);
d[sx][sy]=1;
while(!q.empty()){
node ff=q.front();
q.pop();
int x=ff.x;
int y=ff.y;
if(map[x][y]=='T'){
printf("%d\n",ff.t);
return;
}
for(int i=0;i<4;i++){
x=ff.x+mx[i];
y=ff.y+my[i];
if(!judge(x,y)){
continue;
}
node nt;
if(map[x][y]=='.'||map[x][y]=='T'){//正常走
nt.x=x;
nt.y=y;
nt.t=ff.t+1;
d[nt.x][nt.y]=1;
q.push(nt);
continue;
}else if(i==0||i==2){//右和左
if(map[x][y]=='-'&&ff.t%2==0){//可以过去
nt.x=x+mx[i];
nt.y=y+my[i];
if(judge(nt.x,nt.y)){
nt.t=ff.t+1;
d[nt.x][nt.y]=1;
q.push(nt);
}
}else if(map[x][y]=='|'&&ff.t%2!=0){
nt.x=x+mx[i];
nt.y=y+my[i];
if(judge(nt.x,nt.y)){
nt.t=ff.t+1;
d[nt.x][nt.y]=1;
q.push(nt);
}
}else {//不能过楼梯,得等一分钟
nt.x=ff.x;
nt.y=ff.y;
nt.t=ff.t+1;
d[nt.x][nt.y]=1;
q.push(nt);
}
}else{//上和下
if(map[x][y]=='|'&&ff.t%2==0){//可以过去
nt.x=x+mx[i];
nt.y=y+my[i];
if(judge(nt.x,nt.y)){
nt.t=ff.t+1;
d[nt.x][nt.y]=1;
q.push(nt);
}
}else if(map[x][y]=='-'&&ff.t%2!=0){
nt.x=x+mx[i];
nt.y=y+my[i];
if(judge(nt.x,nt.y)){
nt.t=ff.t+1;
d[nt.x][nt.y]=1;
q.push(nt);
}
}else {//不能过楼梯,得等一分钟
nt.x=ff.x;
nt.y=ff.y;
nt.t=ff.t+1;
d[nt.x][nt.y]=1;
q.push(nt);
}
}
}
}
}
int main(){
while(~scanf("%d%d",&n,&m)){
memset(d,0,sizeof(d));
for(int i=0;i<n;i++){
scanf("%s",map[i]);
for(int j=0;j<m;j++){
if(map[i][j]=='S'){
sx=i,sy=j;
}
}
}
bfs();
}
return 0;
}
下面这一题其实很简单,但当时卡在了前面那一题导致没有ac
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<cmath>
#include<algorithm>
using namespace std;
int map[10][10];///地图
int n,m;
int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};///方向
struct node{
int x;
int y;
int time,step;///时间和当前步数
};
void bfs(int x,int y){///广搜
int i;
queue<node>q;
node now,next;
now.step=0;
now.time=6;
now.x=x;
now.y=y;
q.push(now);
while(!q.empty()){
now=q.front();
q.pop();
for(i=0;i<4;i++){///上下左右都尝试前进
next.x=now.x+dx[i];
next.y=now.y+dy[i];
next.time=now.time-1;
next.step=now.step+1;
if(next.x>=0&&next.x<n&&next.y>=0&&next.y<m&&map[next.x][next.y]!=0&&next.time>0){
if(map[next.x][next.y]==3){///结束点,找到出口
printf("%d\n",next.step);
return ;
}
if(map[next.x][next.y]==4){///倒计时重置
next.time=6;
map[next.x][next.y]=0;
}
q.push(next);
}
}
}
printf("-1\n");///没找到出口就会到这来
return ;
}
int main(){
int i,j,t,sx,sy;
cin>>t;
while(t--){
cin>>n>>m;
for(i=0;i<n;i++){
for(j=0;j<m;j++){
cin>>map[i][j];
if(map[i][j]==2){
sx=i;
sy=j;
}
}
}
bfs(sx,sy);
}
return 0;
}
下面这一题真的是令人头大的一题
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<LL,LL>
const double EPS = 1E-10;
const int MOD = 1E9+7;
const int N = 1000050+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;
struct Edge {
int next;
int to;
int w;
} edge[N*6];
int head[N],tot;
struct Node {
int id;
int dis;
bool operator<(const Node &rhs)const{
return dis>rhs.dis;
}
};
int n,m;
int st,endd;
int dis[N];
bool vis[N];
void addEdge(int x,int y,int w) {
edge[tot].to=y;
edge[tot].w=w;
edge[tot].next=head[x];
head[x]=tot++;
}
void dijkstra() {
n=n*m;
for(int i=0; i<=n; i++)
dis[i]=INF;
memset(vis,0,sizeof(vis));
dis[st]=0;
priority_queue<Node> Q;
Node rec;
rec.id=st;
rec.dis=0;
Q.push(rec);
while(!Q.empty()) {
int u=Q.top().id;
Q.pop();
if(vis[u])
continue;
vis[u]=1;
for(int i=head[u]; i!=-1; i=edge[i].next) {
int v=edge[i].to;
int w=edge[i].w;
if(dis[v]>dis[u]+w) {
dis[v]=dis[u]+w;
Node rec;
rec.id=v;
rec.dis=dis[v];
Q.push(rec);
}
}
}
}
char s[4100][9100];
int main() {
int t;
scanf("%d",&t);
while(t--) {
tot=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
getchar();
int row= m>=2 ? n*4+3 : n*4+1;//字符矩阵总行数
for(int i=1; i<=row; i++)
gets(s[i]);
int cnt1=-m;//i+1%4==0
int cnt2=0;//i-1%4==0
int cnt3=0;//i%4==0
int cnt4=m;//else
for(int i=3; i<=row; i++) {//从第2行开始
if( (i+1)%4==0 ) {
for(int j=0; j<m; j++) {
if(j%2==1) {
if(s[i][j*6+4]==' ') {
addEdge( j+cnt1+1, j+cnt1+m+1, 1 );
addEdge( j+cnt1+m+1, j+cnt1+1, 1 );
}
}
else {
if(s[i][j*6+4]=='S')
st=j+cnt1+m+1;
if(s[i][j*6+4]=='T')
endd=j+cnt1+m+1;
}
}
cnt1+=m;
}
else if( i%4==0 ) {
for(int j=1; j<=m; j++) {
if(s[i][j*6+1]==' ') {
addEdge( cnt2+j, cnt2+j+1,1 );
addEdge( cnt2+j+1, cnt2+j,1 );
}
}
cnt2+=m;
}
else if( (i-1)%4==0 ) {
for(int j=0; j<m; j++) {
if(j%2==1) {
if(s[i][j*6+4]=='S')
st=j+cnt3+1;
if(s[i][j*6+4]=='T')
endd=j+cnt3+1;
}
else {
if(s[i][j*6+4]==' ') {
addEdge( j+cnt3+1, j+cnt3+m+1, 1 );
addEdge( j+cnt3+m+1, j+cnt3+1, 1 );
}
}
}
cnt3+=m;
}
else {
for(int j=1; j<=m; j++) {
if(s[i][j*6+1]==' ') {
if(j%2==1) {
addEdge( cnt4+j, cnt4+j-m+1,1 );
addEdge( cnt4+j-m+1, cnt4+j,1 );
}
else {
addEdge( cnt4-m+j, cnt4+j+1,1 );
addEdge( cnt4+j+1, cnt4-m+j,1 );
}
}
}
cnt4+=m;
}
}
dijkstra();
if(dis[endd]==INF)
puts("-1");
else
printf("%d\n",dis[endd]+1);
}
return 0;
}
-7.10 共勉