标签:rom code eve from color visit point room ann
Note:
Need to skipp the 0 element update room but cannot skip BFS from that point
class Solution { public void wallsAndGates(int[][] rooms) { if (rooms.length == 0 || rooms[0].length == 0) { return; } for (int i = 0; i < rooms.length; i++) { for (int j = 0; j < rooms[i].length; j++) { if (rooms[i][j] == 0) { visit(rooms, i, j, 0); } } } } private void visit(int[][] rooms, int x, int y, int level) { if (rooms[x][y] == -1) { return; } if (level == 0 || rooms[x][y] > level) { if (level != 0) { rooms[x][y] = level; } if (x > 0) visit(rooms, x - 1, y, level + 1); if (x < rooms.length - 1) visit(rooms, x + 1, y, level + 1); if (y > 0) visit(rooms, x, y - 1, level + 1); if (y < rooms[0].length - 1) visit(rooms, x, y + 1, level + 1); } } }
标签:rom code eve from color visit point room ann
原文地址:http://www.cnblogs.com/shuashuashua/p/7489312.html