[uva-572]Oil Deposits (BFS和DFS两种

题目大意:算‘@’连通块个数。
一个格周围八个都算,相邻对角线也算。

注意点是:其实不用visited函数,只搜一次可以优化把搜过的‘@’改成‘*’。

dfs写起来很快。
改成bfs也只花了五分钟不到就过了。
和蔼可亲的一道题。
啪啪啪!
这里写图片描述
现在可以睡啦。
//uva572 (dfs by zhuhua
//Time limit: 3000 ms
//AC time: 0ms???
#include
#include
#include
using namespace std;
char Map[110][110];
bool visited[110][110];
int m,n;
int dx[8]={1,1,0,-1,-1,-1,0,1};
int dy[8]={0,1,1,1,0,-1,-1,-1};

void dfs(int x,int y)
{
    int x2,y2;
    visited[x][y]=true;
    for(int i=0;i<8;i++)
    {
        x2=x+dx[i];
        y2=y+dy[i];
        if(x2>=1&&x2<=m&&y2>=1&&y2<=n
           &&Map[x2][y2]=='@'&&!visited[x2][y2])
           dfs(x2,y2);

    }
}

int main()
{
    int i,j,tot;
    while(cin>>m>>n)
    {
        if(m==0)break;
        for(i=1;i<=m;i++)
        {
            scanf("%s",Map[i]+1);
        }
        memset(visited,0,sizeof(visited));
        tot=0;
        for(i=1;i<=m;i++)
        {
            for(j=1;j<=n;j++)
            {
                if(Map[i][j]=='@'&&!visited[i][j])
                {
                    tot++;
                    dfs(i,j);
                }
            }
        }cout<<tot<<endl;
    }
    return 0;
}

//uva572 (bfs by zhuhua
//Time limit: 3000 ms
//AC time:  0 ms???
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <utility>
using namespace std;
char Map[110][110];
int m,n;
int dx[8]= {1,1,0,-1,-1,-1,0,1};
int dy[8]= {0,1,1,1,0,-1,-1,-1};
typedef pair<int,int> P;

void bfs(int x,int y)
{

    Map[x][y]='*';
    queue <P> que;
    que.push(P(x,y));
    while(!que.empty())
    {
        P out=que.front();
        que.pop();
        for(int i=0; i<8; i++)
        {
            int x2=out.first+dx[i];
            int y2=out.second+dy[i];
            if(x2>=1&&x2<=m&&y2>=1&&y2<=n
                    &&Map[x2][y2]=='@')
            {
                Map[x2][y2]='*';
                que.push(P(x2,y2));
            }

        }
    }

}

int main()
{
    int i,j,tot;
    while(cin>>m>>n)
    {
        if(m==0)break;
        for(i=1; i<=m; i++)
        {
            scanf("%s",Map[i]+1);
        }

        tot=0;
        for(i=1; i<=m; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(Map[i][j]=='@')
                {
                    tot++;
                    bfs(i,j);
                }
            }
        }
        cout<<tot<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,针对第二个问题的线程同步问题,我们可以使用同步方法和ReentrantLock可重入锁两种方式来解决。 ### 1. 同步方法 使用同步方法可以保证在同一时刻只有一个线程能够访问被synchronized关键字修饰的方法。在本例中,我们可以将withdraw和deposit方法都声明为同步方法,如下所示: ```java public synchronized void withdraw(double amount){ if (balance >= amount) { balance -= amount; System.out.println(Thread.currentThread().getName() + " withdraws " + amount + ", balance is " + balance); } else { System.out.println(Thread.currentThread().getName() + " withdraws failed, balance is " + balance); } } public synchronized void deposit(double amount){ if (amount > 0) { balance += amount; System.out.println(Thread.currentThread().getName() + " deposits " + amount + ", balance is " + balance); } else { System.out.println(Thread.currentThread().getName() + " deposits failed, balance is " + balance); } } ``` ### 2. ReentrantLock可重入锁 ReentrantLock可重入锁是一种更加灵活的线程同步方式,它比synchronized关键字提供了更多的功能,如公平锁、可中断锁、多条件变量等。在本例中,我们可以使用ReentrantLock来保证在同一时刻只有一个线程能够访问被锁住的代码块。具体实现如下: ```java private final ReentrantLock lock = new ReentrantLock(); public void withdraw(double amount){ lock.lock(); try { if (balance >= amount) { balance -= amount; System.out.println(Thread.currentThread().getName() + " withdraws " + amount + ", balance is " + balance); } else { System.out.println(Thread.currentThread().getName() + " withdraws failed, balance is " + balance); } } finally { lock.unlock(); } } public void deposit(double amount){ lock.lock(); try { if (amount > 0) { balance += amount; System.out.println(Thread.currentThread().getName() + " deposits " + amount + ", balance is " + balance); } else { System.out.println(Thread.currentThread().getName() + " deposits failed, balance is " + balance); } } finally { lock.unlock(); } } ``` 在以上代码中,我们使用了ReentrantLock的lock()和unlock()方法来手动加锁和解锁代码块。使用try-finally语句块的方式可以确保即使在代码块执行过程中出现异常,锁也能够被正确释放。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值