• A-Plague Inc


    A-Plague Inc

    题意:n*m个城市,给你初始几个感染病毒的城市坐标,每天这个城市都可以感染上下左右4个相临城市,问最后被感染的城市坐标,如果有多个,取x坐标小的,如果x坐标一样大,取y坐标小的。

    题解:我是用的队列,可能做得麻烦了,先根据队列中的元素求出下一天感染的城市坐标,每次将新感染的城市入队,同时前一天感染的城市出队(注意要出干净,我就是干开始没出干净wa了几发)

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    const int maxn = 2000 + 10;
    int vis[maxn][maxn];
    int main()
    {
    	//freopen("in.txt", "r", stdin);
    	int n, m;
    	while (scanf("%d%d", &n, &m) != EOF)
    	{
    		int num;
    		pair<int, int>ans;
    		memset(vis, 0, sizeof vis);
    		scanf("%d", &num);
    		queue<pair<int, int> > q;
    		ll sum = num;
    		for (int i = 0; i < num; i++)
    		{
    			int x, y;
    			scanf("%d%d", &x, &y);
    			q.push(make_pair(x, y));
    			vis[x][y] = 1;
    		}
                    //没有全部感染则一直入队
    		while (sum < n * m)
    		{
    			int tmp = q.size();
                            //根据前一天的感染城市求出4个方向新感染的城市
    			for (int i = 0; i < tmp; i++)
    			{    
    				if (q.front().second + 1 <= m && !vis[q.front().first][q.front().second + 1])
    				{
    					q.push(make_pair(q.front().first, q.front().second + 1));
    					vis[q.front().first][q.front().second + 1] = 1;
    					sum++;
    				}
    				if (q.front().second - 1 > 0 && !vis[q.front().first][q.front().second - 1])
    				{
    					q.push(make_pair(q.front().first, q.front().second - 1));
    					vis[q.front().first][q.front().second - 1] = 1;
    					sum++;
    				}
    				if (q.front().first + 1 <= n && !vis[q.front().first + 1][q.front().second])
    				{
    					q.push(make_pair(q.front().first + 1, q.front().second));
    					vis[q.front().first + 1][q.front().second] = 1;
    					sum++;
    				}
    				if (q.front().first - 1 > 0 && !vis[q.front().first - 1][q.front().second])
    				{
    					q.push(make_pair(q.front().first - 1, q.front().second));
    					vis[q.front().first - 1][q.front().second] = 1;
    					sum++;
    				}
    				q.pop();//用完出队
    			}
    		}
    		ans = q.front();
                    //比较最后一次入队元素中满足条件的答案
    		while (!q.empty())
    		{
    
    			if (ans.first > q.front().first)
    				ans = q.front();
    			else if (ans.first == q.front().first && q.front().second < ans.second)
    				ans = q.front();
    			q.pop();
    		}
    		printf("%d %d
    ", ans.first, ans.second);
    	}
    	return 0;
    }
    
  • 相关阅读:
    PHP无限极分类生成树方法
    如何用c#本地代码实现与Webbrowser中的JavaScript交互
    在sqlite中,如何删除字段? how to drop a column in sqlite
    如何在centos下配置redis开机自启动
    WinForm中的图表控件Chart
    XPath高级用法(冰山一角)
    windows下MySQL的安装(非安装包)
    WinFrom下Webbrowser加载自定义页面的技巧
    .net下使用最小堆实现TopN算法
    .net程序中http请求的超时配置
  • 原文地址:https://www.cnblogs.com/kuroko-ghh/p/9363411.html
Copyright © 2020-2023  润新知