leetcode773
使用两种语言实现,先贴C++的
class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int newColor) {
        int row = image.size();
        int col = image[].size();
        int oldColor = image[sr][sc];
        if (oldColor == newColor)
        {
            return image;
        }
        queue<pair<int, int>> Q;
        Q.push(make_pair(sr, sc));
        const int N = ;
        int Visited[N][N];
        for (int i = ; i < row; i++)
        {
            for (int j = ; j < col; j++)
            {
                Visited[i][j] = false;
            }
        }
        while (!Q.empty())
        {
            pair<int, int> Node = Q.front();
            Q.pop();
            int x = Node.first;
            int y = Node.second;
            if (Visited[x][y])
            {
                continue;//略过此点
            }
            else
            {
                Visited[x][y] = true;
                image[x][y] = newColor;
            }
            pair<int, int> Up_Node = make_pair(x - , y);
            pair<int, int> Down_Node = make_pair(x + , y);
            pair<int, int> Left_Node = make_pair(x, y - );
            pair<int, int> Right_Node = make_pair(x, y + );
            if (Up_Node.first >=  && !Visited[Up_Node.first][Up_Node.second] && image[Up_Node.first][Up_Node.second] == oldColor)
            {
                Q.push(Up_Node);
            }
            if (Down_Node.first <= row -  && !Visited[Down_Node.first][Down_Node.second] && image[Down_Node.first][Down_Node.second] == oldColor)
            {
                Q.push(Down_Node);
            }
            if (Left_Node.second >=  && !Visited[Left_Node.first][Left_Node.second] && image[Left_Node.first][Left_Node.second] == oldColor)
            {
                Q.push(Left_Node);
            }
            if (Right_Node.second <= col -  && !Visited[Right_Node.first][Right_Node.second] && image[Right_Node.first][Right_Node.second] == oldColor)
            {
                Q.push(Right_Node);
            }
        }
        return image;
    }
};
下面贴出C#的
public class Solution
{
public int[,] FloodFill(int[,] image, int sr, int sc, int newColor)
{
int row = image.GetLength();
int col = image.GetLength(); int oldColor = image[sr, sc];
if (oldColor == newColor)
{
return image;
}
Queue<KeyValuePair<int, int>> Q = new Queue<KeyValuePair<int, int>>();
Q.Enqueue(new KeyValuePair<int, int>(sr, sc)); bool[,] Visited = new bool[row, col]; while (Q.Any())
{
var Node = Q.Dequeue();
int x = Node.Key;
int y = Node.Value;
if (Visited[x, y])
{
continue;//略过此点
}
else
{
Visited[x, y] = true;
image[x, y] = newColor;
}
var Up_Node = new KeyValuePair<int, int>(x - , y);
var Down_Node = new KeyValuePair<int, int>(x + , y);
var Left_Node = new KeyValuePair<int, int>(x, y - );
var Right_Node = new KeyValuePair<int, int>(x, y + ); if (Up_Node.Key >= && !Visited[Up_Node.Key, Up_Node.Value] && image[Up_Node.Key, Up_Node.Value] == oldColor)
{
Q.Enqueue(Up_Node);
} if (Down_Node.Key <= row - && !Visited[Down_Node.Key, Down_Node.Value] && image[Down_Node.Key, Down_Node.Value] == oldColor)
{
Q.Enqueue(Down_Node);
} if (Left_Node.Value >= && !Visited[Left_Node.Key, Left_Node.Value] && image[Left_Node.Key, Left_Node.Value] == oldColor)
{
Q.Enqueue(Left_Node);
} if (Right_Node.Value <= col - && !Visited[Right_Node.Key, Right_Node.Value] && image[Right_Node.Key, Right_Node.Value] == oldColor)
{
Q.Enqueue(Right_Node);
}
} return image;
}
}
leetcode773的更多相关文章
- [Swift]LeetCode773. 滑动谜题 | Sliding Puzzle
		
On a 2x3 board, there are 5 tiles represented by the integers 1 through 5, and an empty square repre ...
 
随机推荐
- 【Hive】自定义函数
			
Hive的自定义函数无法满足实际业务的需要,所以为了扩展性,Hive官方提供了自定义函数来实现需要的业务场景. 1.定义 (1)udf(user defined function): 自定义函数,特 ...
 - 019对象——对象 method_exists property_exists instanceof
			
<?php /** * 19 对象 method_exists property_exists instanceof */ //method_exists() 判断方法是否存在,第一个参数对象或 ...
 - Puzzle Game HihoCoder - 1634
			
题目链接:https://cn.vjudge.net/problem/HihoCoder-1634 题目意思:可以让矩阵里的某一个数变成p,或者不修改.求最大子矩阵最小,输出最小值. 思路:请看下图 ...
 - Arcgis for Androd API开发系列教程(一)——地图显示与GPS定位
			
序:最近呢,工作鸭梨不是怎么大,对于自己爱折腾的想法又冒出了水面,开始自己的android开发的学习之旅.但是呢,本人是做GIS的,所以呢,就打算从这方面入手看看,是不是有什么比较好玩的玩意呢,这才导 ...
 - Go语言使用匿名结构体解析JSON数据
			
手机拥有屏幕.电池.指纹识别等信息,将这些信息填充为 JSON 格式的数据.如果需要选择性地分离 JSON 中的数据则较为麻烦.Go 语言中的匿名结构体可以方便地完成这个操作. 首先给出完整的代码,然 ...
 - linux多线程全面解析
			
引入: 在传统的Unix模型中,当一个进程需要由另一个实体执行某件事时,该进程派生(fork)一个子进程,让子进程去进行处理.Unix下的大多数网络服务器程序都是这么编写的,即父进程接受连 ...
 - (十一)java循环结构
			
while(循环的条件) {循环的语句} int a = 1; while(a < 5) { System.out.println(a);//1,2,3,4 a++; } System.out. ...
 - NOIP2011 观光公交 加强版
			
传送门 题目大意 给定从左到右的$n$个车站以及两两之间通行的需要的时间. 有$m$个人,第$i$个人会在$T_i$时刻出现在$a_i$车站,目的地是$b_i$. 一辆车第$0$时刻出现在一号站台,从 ...
 - python3  tesserocr 安装 来解决部分爬虫遇到的字符识别问题
			
1. OCR OCR,即Optical Character Recognition,光学字符识别,是指通过扫描字符,然后通过其形状将其翻译成电子文本的过程.对于图形验证码来说,它们都是一些不规则的字符 ...
 - CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\....\App_Web_default.aspx.cdcab7d2.zii776dc.dll”--“拒绝访问。 ”
			
关于访问asp.net站点出现以下问题的解决办法: 问题: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework\v4.0.30319\Tempora ...