NEUQ-ACM预备队必做题

B3625 迷宫寻路

bfs

#include <bits/stdc++.h>
#define f(i,x,y) for(int (i)=(x);(i)<=(y);++(i)) 
#define ll long long
#define check(x) (x)=='#'?0:1  
using namespace std;
char a[101][101];
int con[101][101];
int n,m;
int op[4][2]={1,0,
              -1,0,
              0,1,
              0,-1};
void dfs(int x,int y);
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    f(i,1,n)
        f(j,1,m)
            cin>>a[i][j];  
    if(check(a[1][1]))  
    {
        stack<pair<int, int>> st;
        st.push({1, 1});
        while (!st.empty()) {
            int x = st.top().first;
            int y = st.top().second;
            st.pop();
            if (x == n && y == m) {
                cout << "Yes";
                return 0;
            }
            if (con[x][y] == 1) {
                continue;
            }
            con[x][y] = 1;
            for(int i = 0; i < 4; ++i) {
                int x1 = op[i][0] + x, y1 = op[i][1] + y;
                if (x1 > 0 && x1 <= n && y1 > 0 && y1 <= m) {
                    if (check(a[x1][y1]) && con[x1][y1] == 0) {
                        st.push({x1, y1});
                    }
                }
            }
        }
    }
    cout<<"No";  
    return 0; 
}

P1706 全排列问题

行宽,注意dfs

#include <bits/stdc++.h>
#define f(i,x,y) for(int (i)=(x);(i)<=(y);++(i)) 
#define ll long long
#define check(x) (x)=='#'?0:1  
using namespace std;
int n;  
int a[10]={0},con[10]={0};
void putout()
{
	ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	f(i,1,n)
	  cout<<setw(5)<<a[i];
	cout<<'\n';  
	return;
}
void dps(int step)
{
	if(step==n+1){
	  	putout();
	  	return;
	  }
	f(i,1,n)
	  if(con[i]==0){
	  	con[i]=1;
	  	a[step]=i;
	  	dps(step+1);
	  	con[i]=0;
	  }
	return;  
	    
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    dps(1);
    return 0; 
}

P1451 求细胞数量

遍历每一个点,标记数过的细胞bfs

#include <bits/stdc++.h>
#define f(i,x,y) for(int (i)=(x);(i)<=(y);++(i)) 
#define ll long long 
using namespace std;
int n,m,ans=0;  
int con[101][101]={0};
char a[101][101];
int op[4][2]={0,1,
              0,-1,
              1,0,
              -1,0};
void bfs(int x,int y)
{
	queue<int> x1,y1;
	x1.push(x),y1.push(y);
	while(!x1.empty())
	{
		int x0=x1.front(),y0=y1.front();
		x1.pop(),y1.pop();
		f(i,0,3)
		{
			int now_x=x0+op[i][0],now_y=y0+op[i][1];
			if(now_x>0&&now_x<=n&&now_y>0&&now_y<=m&&con[now_x][now_y]){
				x1.push(now_x),y1.push(now_y);
				con[now_x][now_y]=0;
			}
		}
	}
	return;
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=n;++i){
      for(int j=1;j<=m;++j){
        cin >> a[i][j];
        if(a[i][j]!='0')
        con[i][j]=1;
      }
    }      
	
	for(int i=1;i<=n;++i)
      for(int j=1;j<=m;++j){
      	if(con[i][j]){
      		bfs(i,j);
      		++ans;	
		  }
	  }  
    cout<<ans;   	
    return 0; 
}

P1219 [USACO1.5] 八皇后 Checker Challenge

注意怎么标记斜边,bfs

#include <bits/stdc++.h>
#define f(i,x,y) for(int (i)=(x);(i)<=(y);++(i)) 
#define ll long long 
#define lay1(a,b) (a)+(b)-1
#define lay2(a,b) n-(a)+(b)
using namespace std;
int n,ans=0;
int con_row[150],con_c[150],con_lay1[150],con_lay2[150];// 不用row,走row换 
int a[15][15];
int op[4][2]={0,1,
              0,-1,
              1,0,
              -1,0};                
void putout()
{
	f(i,1,n)
	    f(j,1,n)
	      if(a[i][j]){
	      	cout<<j<<' ';
	      	break;
		  }
	cout<<'\n';    
	return;    
}
void dfs(int y)
{
	if(y==n+1){
		++ans;
		if(ans<=3)
		  putout();
		return;  
	}
	f(i,1,n)
	    if(!con_c[i]&&!con_lay1[lay1(i,y)]&&!con_lay2[lay2(i,y)]){
	  	    con_c[i]=1,con_lay1[lay1(i,y)]=1,con_lay2[lay2(i,y)]=1;
	  	    a[y][i]=1;
	  	    dfs(y+1);
	  	    con_c[i]=0,con_lay1[lay1(i,y)]=0,con_lay2[lay2(i,y)]=0;
	  	    a[y][i]=0;
		} 
}
int main()
{
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    dfs(1); 
    cout<<ans;   	
    return 0; 
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值