0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Leetcode Easy 994. Rotting Oranges

Posted at

解法
腐っている状態を1として腐った状態を2とする。
1秒毎に腐ったミカンの周囲を腐らせる。
While(1)で1秒毎の処理を記述する
状態2の周囲が状態1であれば状態2にする。

1秒ごとに変化が起きているかどうかをBool型でチェックする
変化が起きなくなった時、すべて状態2になれば終了
その時、状態1のオレンジが存在する場合は-1を返す

using namespace std;
# include <iostream>
# include <algorithm>
# include <vector>
# include <random>
typedef long long  ll;
# define rep(i,s,n)for(ll i=s;i<n;i++)
# define repe(i,s,n)for(ll i=s;i<=n;i++)

class Solution {
public:
    int orangesRotting(vector<vector<int>>& grid) {
        int row = grid.size();
        int col = grid[0].size();
        bool v[100][100];
        rep(i, 0, 100) {
            rep(j, 0, 100) {
                    v[i][j] = false;
            }
        }

        int ans = 0;
        int x[4] = {1,0,0,-1};
        int y[4] = {0,1,-1,0};
        while (1)
        {
            bool changed = false;
            rep(i, 0, row) {
                rep(j, 0, col) {
                    if (grid[i][j] == 2 && !v[i][j]) {
                        v[i][j] = true;
                        rep(k, 0, 4) {
                            int ny = i + y[k];
                            int nx = j + x[k];

                            if (ny >= 0 && nx >= 0 && ny < row && nx < col && !v[ny][nx] && grid[ny][nx] == 1) {
                                changed = true;
                                grid[ny][nx] = 2;
                                v[ny][nx] = true;
                            }
                        }
                    }
                }
            }
            if (changed) {
                ans++;
                rep(i, 0, 100) {
                    rep(j, 0, 100) {
                        v[i][j] = false;
                    }
                }
            }
            else {
                rep(i, 0, row) {
                    rep(j, 0, col) {
                        if (grid[i][j] == 1) {
                            return -1;
                        }
                    }
                }
                return ans;
            }
        }

        return -1;
    }
};

//int main()
//{
//    Solution* Sol = new Solution();
//    random_device rnd;
//    mt19937 mt(rnd());
//
//    vector<int> v1(300);
//    for (int i = 0; i < 100; ++i) v1[i] = (mt() % 1000);
//
//    cout << endl;
//    vector<int> v2(100);
//    for (int i = 0; i < 100; ++i) v2[i] = mt() % 1000;
//
//    //vector<vector<int>> grid = { { 2, 1, 1} ,{1, 1, 0},{0, 1, 1} };
//    vector<vector<int>> grid = { { 2, 1, 1} ,{0, 1, 1},{1, 0, 1} };
//    cout <<Sol->orangesRotting(grid);
//}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?