zoukankan      html  css  js  c++  java
  • poj3620

    这是一道简单题,但是我错了n次。

    开数组的时候一定要注意范围,不要直接maxn;

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    using namespace std;
    
    const int maxn = 101;
    
    struct point
    {
    	int x, y;
    }cell[maxn * maxn];
    
    int n, m, k;
    int dir[4][2] = {{1, 0},{-1, 0},{0, 1},{0, -1}};
    int map[maxn][maxn];
    bool vis[maxn * maxn];
    
    void init()
    {
    	memset(map, -1, sizeof(map));
    	memset(vis, 0, sizeof(vis));
    	for (int i = 0; i < k; i++)
    	{
    		scanf("%d%d", &cell[i].x, &cell[i].y);
    		cell[i].x--;
    		cell[i].y--;
    		map[cell[i].x][cell[i].y] = i;
    	}
    }
    
    bool ok(point &a)
    {
    	if (a.x < 0 || a.y < 0 || a.y >= m || a.x >=n)
    		return false;
    	if (map[a.x][a.y] == -1)
    		return false;
    	if (vis[map[a.x][a.y]])
    		return false;
    	return true;
    }
    
    int bfs(point &s)
    {
    	int front = 0, rear = 0;
    	point q[maxn * maxn];
    	int r = 1;
    
    	vis[map[s.x][s.y]] = true;
    	q[front++] = s;
    	while (front != rear)
    	{
    		point b = q[rear++];
    		if (rear == maxn * maxn)
    			rear = 0;
    		for (int i = 0; i < 4; i++)
    		{
    			point a;
    			a.x = b.x + dir[i][0];
    			a.y = b.y + dir[i][1];
    			if (ok(a))
    			{
    				q[front++] = a;
    				if (front == maxn * maxn)
    					front = 0;
    				r++;
    				vis[map[a.x][a.y]] = true;
    			}
    		}
    	}
    	return r;
    }
    
    int main()
    {
    	int ans = 0;
    	//freopen("D:\\t.txt", "r", stdin);
    	scanf("%d%d%d", &n, &m, &k);
    	init();
    	for (int i = 0; i < k; i++)
    		if (!vis[i])
    		{
    			int temp = bfs(cell[i]);
    			if (ans < temp)
    				ans = temp;
    		}
    	printf("%d\n", ans);
    	return 0;
    }
  • 相关阅读:
    [php-src]一个Php扩展的结构
    告别2015,迎来2016
    [JS]应用splice删除多元素时出现的坑
    [Ng]Angular应用点概览
    [MongoDB]Mongodb攻略
    GNU M4
    [Linux]服务管理:RPM包, 源码包
    [Shell]条件判断与流程控制:if, case, for, while, until
    [Shell]字符截取命令:cut, printf, awk, sed
    [Shell]正则表达式与通配符
  • 原文地址:https://www.cnblogs.com/rainydays/p/1948658.html
Copyright © 2011-2022 走看看