{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2026-05-26T00:16:20.000Z","description":"Problems submitted by members of the MATLAB Central community.","is_default":true,"created_by":161519,"badge_id":null,"featured":false,"trending":false,"solution_count_in_trending_period":0,"trending_last_calculated":"2026-05-26T00:00:00.000Z","image_id":null,"published":true,"community_created":false,"status_id":2,"is_default_group_for_player":false,"deleted_by":null,"deleted_at":null,"restored_by":null,"restored_at":null,"description_opc":null,"description_html":null,"published_at":null},"problems":[{"id":45395,"title":"Create a matrix map of increasing safety levels","description":"The sole nuclear power plant at Grid City suddenly had a meltdown. Luckily, the plant was designed to be in full automation, so no casualties were ever recorded. However, thanks to radiation, the whole city is inhabitable for close to a century.\r\n\r\nYou are then given a portion of the map of Grid City which, for the purpose of this problem, is rendered as an N x N matrix. As the name suggests, Grid City is made of a grid of cells. Your task is to assign a safety level to each cell in this matrix, according to the following rules:\r\n\r\n# Assign _Safety Level 1_ to the cell location of the nuclear power plant, given at row R and column C. This signifies that cell (R,C) is least safe.\r\n# Assign _Safety Level 2_ to the adjacent cells around the power plant, signifying a safer radiation level in this area.\r\n# Continue assigning an increasing number of _Safety Levels_ at succeeding layers of cells surrounding the power plant until you reach the edge of the map.\r\n\r\nWrite a function that accepts three inputs, N, R, and C, and outputs a matrix map of Grid City with _Safety Levels_. Note that you can use any method of populating the matrix as long as it gives the correct answer. You are also ensured that:\r\n\r\n* N, R, C are all integers\r\n* 1 \u003c= N \u003c= 10\r\n* 1 \u003c= R \u003c= N and 1 \u003c= C \u003c= N\r\n\r\nHere are a few examples:\r\n\r\n  \u003e\u003e safety_map(5,5,4) % N = 5, R = 5, C = 4\r\n  ans =\r\n\r\n     5     5     5     5     5\r\n     4     4     4     4     4\r\n     4     3     3     3     3\r\n     4     3     2     2     2\r\n     4     3     2     1     2\r\n\u003e\u003e safety_map(6,2,3) % N = 6, R = 2, C = 3\r\nans =\r\n\r\n     3     2     2     2     3     4\r\n     3     2     1     2     3     4\r\n     3     2     2     2     3     4\r\n     3     3     3     3     3     4\r\n     4     4     4     4     4     4\r\n     5     5     5     5     5     5\r\n","description_html":"\u003cp\u003eThe sole nuclear power plant at Grid City suddenly had a meltdown. Luckily, the plant was designed to be in full automation, so no casualties were ever recorded. However, thanks to radiation, the whole city is inhabitable for close to a century.\u003c/p\u003e\u003cp\u003eYou are then given a portion of the map of Grid City which, for the purpose of this problem, is rendered as an N x N matrix. As the name suggests, Grid City is made of a grid of cells. Your task is to assign a safety level to each cell in this matrix, according to the following rules:\u003c/p\u003e\u003col\u003e\u003cli\u003eAssign \u003ci\u003eSafety Level 1\u003c/i\u003e to the cell location of the nuclear power plant, given at row R and column C. This signifies that cell (R,C) is least safe.\u003c/li\u003e\u003cli\u003eAssign \u003ci\u003eSafety Level 2\u003c/i\u003e to the adjacent cells around the power plant, signifying a safer radiation level in this area.\u003c/li\u003e\u003cli\u003eContinue assigning an increasing number of \u003ci\u003eSafety Levels\u003c/i\u003e at succeeding layers of cells surrounding the power plant until you reach the edge of the map.\u003c/li\u003e\u003c/ol\u003e\u003cp\u003eWrite a function that accepts three inputs, N, R, and C, and outputs a matrix map of Grid City with \u003ci\u003eSafety Levels\u003c/i\u003e. Note that you can use any method of populating the matrix as long as it gives the correct answer. You are also ensured that:\u003c/p\u003e\u003cul\u003e\u003cli\u003eN, R, C are all integers\u003c/li\u003e\u003cli\u003e1 \u0026lt;= N \u0026lt;= 10\u003c/li\u003e\u003cli\u003e1 \u0026lt;= R \u0026lt;= N and 1 \u0026lt;= C \u0026lt;= N\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eHere are a few examples:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; safety_map(5,5,4) % N = 5, R = 5, C = 4\r\nans =\r\n\u003c/pre\u003e\u003cpre\u003e     5     5     5     5     5\r\n     4     4     4     4     4\r\n     4     3     3     3     3\r\n     4     3     2     2     2\r\n     4     3     2     1     2\r\n\u0026gt;\u0026gt; safety_map(6,2,3) % N = 6, R = 2, C = 3\r\nans =\u003c/pre\u003e\u003cpre\u003e     3     2     2     2     3     4\r\n     3     2     1     2     3     4\r\n     3     2     2     2     3     4\r\n     3     3     3     3     3     4\r\n     4     4     4     4     4     4\r\n     5     5     5     5     5     5\u003c/pre\u003e","function_template":"function y = safety_map(N,R,C)\r\n  y = N;\r\nend","test_suite":"%%\r\ny_correct = [3 2 2 2 3 4; 3 2 1 2 3 4; ...\r\n3 2 2 2 3 4; 3 3 3 3 3 4; 4 4 4 4 4 4; ...\r\n5 5 5 5 5 5];\r\nassert(isequal(safety_map(6,2,3),y_correct))\r\n%%\r\ny_correct = [2 2;2 1];\r\nassert(isequal(safety_map(2,2,2),y_correct))\r\n%%\r\ny_correct = [9 8 7 6 5 4 3 2 1 2; ...\r\n9 8 7 6 5 4 3 2 2 2; 9 8 7 6 5 4 3 3 3 3; ...\r\n9 8 7 6 5 4 4 4 4 4; 9 8 7 6 5 5 5 5 5 5; ...\r\n9 8 7 6 6 6 6 6 6 6; 9 8 7 7 7 7 7 7 7 7; ...\r\n9 8 8 8 8 8 8 8 8 8; 9 9 9 9 9 9 9 9 9 9; ...\r\n10 10 10 10 10 10 10 10 10 10];\r\nassert(isequal(safety_map(10,1,9),y_correct))\r\n%%\r\ny_correct = 1;\r\nassert(isequal(safety_map(1,1,1),y_correct))\r\n%%\r\ny_correct = [4 3 2 2; ...\r\n4 3 2 1; 4 3 2 2; 4 3 3 3];\r\nassert(isequal(safety_map(4,2,4),y_correct))\r\n%%\r\ny_correct = [2 1;2 2];\r\nassert(isequal(safety_map(2,1,2),y_correct))\r\n%%\r\ny_correct = [4 4 4 4 4 4 4; 4 3 3 3 3 3 4; ...\r\n4 3 2 2 2 3 4; 4 3 2 1 2 3 4; 4 3 2 2 2 3 4; ...\r\n4 3 3 3 3 3 4; 4 4 4 4 4 4 4];\r\nassert(isequal(safety_map(7,4,4),y_correct))\r\n%%\r\ny_correct = [10 10 10 10 10 10 10 10 10 10; ...\r\n10 9 9 9 9 9 9 9 9 9; 10 9 8 8 8 8 8 8 8 8; ...\r\n10 9 8 7 7 7 7 7 7 7; 10 9 8 7 6 6 6 6 6 6; ...\r\n10 9 8 7 6 5 5 5 5 5; 10 9 8 7 6 5 4 4 4 4; ...\r\n10 9 8 7 6 5 4 3 3 3; 10 9 8 7 6 5 4 3 2 2; ...\r\n10 9 8 7 6 5 4 3 2 1];\r\nassert(isequal(safety_map(10,10,10),y_correct))\r\n%%\r\ny_correct = [3 3 3; 2 2 3; 1 2 3];\r\nassert(isequal(safety_map(3,3,1),y_correct))\r\n%%\r\ny_correct = [2 1 2; 2 2 2; 3 3 3];\r\nassert(isequal(safety_map(3,1,2),y_correct))\r\n","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":92,"test_suite_updated_at":"2020-03-27T15:28:42.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-03-27T15:22:51.000Z","updated_at":"2026-05-03T02:49:25.000Z","published_at":"2020-03-27T15:22:51.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe sole nuclear power plant at Grid City suddenly had a meltdown. Luckily, the plant was designed to be in full automation, so no casualties were ever recorded. However, thanks to radiation, the whole city is inhabitable for close to a century.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are then given a portion of the map of Grid City which, for the purpose of this problem, is rendered as an N x N matrix. As the name suggests, Grid City is made of a grid of cells. Your task is to assign a safety level to each cell in this matrix, according to the following rules:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAssign\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSafety Level 1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e to the cell location of the nuclear power plant, given at row R and column C. This signifies that cell (R,C) is least safe.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAssign\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSafety Level 2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e to the adjacent cells around the power plant, signifying a safer radiation level in this area.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eContinue assigning an increasing number of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSafety Levels\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e at succeeding layers of cells surrounding the power plant until you reach the edge of the map.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that accepts three inputs, N, R, and C, and outputs a matrix map of Grid City with\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSafety Levels\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Note that you can use any method of populating the matrix as long as it gives the correct answer. You are also ensured that:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eN, R, C are all integers\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1 \u0026lt;= N \u0026lt;= 10\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1 \u0026lt;= R \u0026lt;= N and 1 \u0026lt;= C \u0026lt;= N\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHere are a few examples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[\u003e\u003e safety_map(5,5,4) % N = 5, R = 5, C = 4\\nans =\\n\\n     5     5     5     5     5\\n     4     4     4     4     4\\n     4     3     3     3     3\\n     4     3     2     2     2\\n     4     3     2     1     2\\n\u003e\u003e safety_map(6,2,3) % N = 6, R = 2, C = 3\\nans =\\n\\n     3     2     2     2     3     4\\n     3     2     1     2     3     4\\n     3     2     2     2     3     4\\n     3     3     3     3     3     4\\n     4     4     4     4     4     4\\n     5     5     5     5     5     5]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":46028,"title":"Paint it black!","description":"\u003chttps://en.wikipedia.org/wiki/Flood_fill Flood fill\u003e is the algorithm used by bucket-fill tools in many image editors. The idea is that all connected pixels with the input color should have it changed to an output color starting at a pixel P = (x,y). Your task is to implement this algorithm replacing all colors within some tolerance (distance) from the input color with black. \r\n\r\nFor instance:\r\n\r\nif the input_color=9, tolerance = 0, and P=[1,1], then the following grayscale image,\r\n\r\n  9 9 9 2 9\r\n  9 9 9 2 9\r\n  2 2 2 9 9 \r\n  9 9 9 9 9\r\n\r\nbecomes,\r\n\r\n  0 0 0 2 9\r\n  0 0 0 2 9\r\n  2 2 2 9 9 \r\n  9 9 9 9 9\r\n\r\nAssume that all pixels have \u003chttps://en.wikipedia.org/wiki/Pixel_connectivity#4-connected 4-neighborhoods\u003e. And use the \u003chttps://en.wikipedia.org/wiki/Taxicab_geometry Manhattan distance\u003e to decide if a color, a 1xn vector, is within tolerance. ","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Flood_fill\"\u003eFlood fill\u003c/a\u003e is the algorithm used by bucket-fill tools in many image editors. The idea is that all connected pixels with the input color should have it changed to an output color starting at a pixel P = (x,y). Your task is to implement this algorithm replacing all colors within some tolerance (distance) from the input color with black.\u003c/p\u003e\u003cp\u003eFor instance:\u003c/p\u003e\u003cp\u003eif the input_color=9, tolerance = 0, and P=[1,1], then the following grayscale image,\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e9 9 9 2 9\r\n9 9 9 2 9\r\n2 2 2 9 9 \r\n9 9 9 9 9\r\n\u003c/pre\u003e\u003cp\u003ebecomes,\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e0 0 0 2 9\r\n0 0 0 2 9\r\n2 2 2 9 9 \r\n9 9 9 9 9\r\n\u003c/pre\u003e\u003cp\u003eAssume that all pixels have \u003ca href = \"https://en.wikipedia.org/wiki/Pixel_connectivity#4-connected\"\u003e4-neighborhoods\u003c/a\u003e. And use the \u003ca href = \"https://en.wikipedia.org/wiki/Taxicab_geometry\"\u003eManhattan distance\u003c/a\u003e to decide if a color, a 1xn vector, is within tolerance.\u003c/p\u003e","function_template":"function J = floodfill(I, p, input_color, tolerance)\r\n  J = I;\r\nend","test_suite":"%%\r\nfiletext = fileread('floodfill.m');\r\nassert(isempty(strfind(filetext, 'eval')),       'Eval forbidden.');\r\nassert(isempty(strfind(filetext, 'regexp')),     'Regexp forbidden.');\r\nassert(isempty(strfind(filetext, '!')),          'Shell commands are forbidden.');\r\nassert(isempty(strfind(filetext, 'mlock')),      'mlock is forbidden.');\r\nassert(isempty(strfind(filetext, 'munlock')),    'munlock is forbidden.');\r\n%%\r\np = [1, 1];\r\ninput_color = 9;\r\ntolerance = 0;\r\nI =[9     9     9     2     9;\r\n    9     9     9     2     9;\r\n    2     2     2     9     9;\r\n    9     9     9     9     9];\r\ny_correct = [105\t177\t73\t204\t122\t130\t214\t108\t173\t122\t154\t247\t132\t149\t158\t137\t49\t171\t159\t188\t197\t146\t104\t212\t84\t234\t120\t67\t160\t122\t71\t32\t47\t185\t5\t209\t49\t198\t215\t117\t128\t125\t185\t181\t102\t14\t159\t250\t201\t95\t158\t167\t242\t21\t94\t99\t214\t253\t223\t168\t146\t133\t126\t232];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J)).digest()) == y_correct));\r\n\r\n%%\r\np = [115, 133];\r\ninput_color = 1;\r\ntolerance = 0;\r\nI=imread(fullfile(matlabroot,'toolbox','images','imdata','blobs.png'));\r\ny_correct = [135\t217\t59\t179\t157\t208\t161\t95\t218\t159\t210\t244\t62\t237\t115\t179\t200\t252\t175\t39\t230\t197\t55\t36\t8\t79\t77\t168\t253\t109\t230\t204\t158\t114\t20\t231\t91\t63\t169\t114\t57\t73\t121\t96\t51\t204\t92\t136\t34\t220\t205\t158\t222\t148\t80\t205\t117\t32\t220\t80\t220\t62\t195\t26];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J)).digest()) == y_correct));\r\n\r\n%%\r\np = [170, 70];\r\ninput_color = 1;\r\ntolerance = 0;\r\nI=imread(fullfile(matlabroot,'toolbox','images','imdata','blobs.png'));\r\ny_correct = [45\t21\t215\t148\t24\t198\t184\t203\t33\t38\t222\t107\t60\t205\t211\t33\t76\t33\t62\t139\t133\t58\t42\t238\t77\t128\t243\t214\t143\t201\t181\t109\t218\t80\t3\t41\t44\t130\t179\t21\t51\t87\t168\t161\t118\t110\t241\t214\t236\t45\t210\t130\t28\t94\t170\t39\t9\t240\t10\t103\t158\t207\t214\t203];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J)).digest()) == y_correct));\r\n\r\n%%\r\np = [1500, 1840];\r\ninput_color = 204;\r\ntolerance = 5;\r\nI=imread(fullfile(matlabroot,'toolbox','images','imdata','concordorthophoto.png'));\r\ny_correct = [180\t96\t232\t179\t100\t190\t0\t55\t194\t91\t49\t45\t220\t214\t233\t3\t164\t217\t254\t197\t27\t26\t207\t107\t116\t80\t153\t76\t121\t137\t231\t21\t236\t37\t200\t112\t254\t5\t121\t70\t181\t182\t106\t13\t39\t235\t188\t82\t140\t107\t118\t11\t163\t102\t78\t97\t230\t36\t252\t46\t165\t126\t85\t17];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J)).digest()) == y_correct));\r\n\r\n%%\r\np = [375, 635];\r\ninput_color = [202, 67, 37];\r\ntolerance = 175;\r\nI=imread(fullfile(matlabroot,'toolbox','images','imdata','flamingos.jpg'));\r\ny_correct = [30\t139\t106\t7\t97\t110\t190\t130\t24\t218\t242\t177\t34\t121\t66\t214\t96\t87\t10\t195\t170\t9\t145\t230\t222\t27\t243\t72\t82\t124\t63\t198\t151\t209\t215\t250\t32\t44\t207\t179\t112\t121\t173\t232\t193\t144\t31\t238\t152\t205\t145\t57\t41\t194\t8\t221\t119\t157\t210\t6\t83\t112\t142\t99];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J(:))).digest()) == y_correct));\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":6,"created_by":443343,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":3,"test_suite_updated_at":"2020-07-19T21:02:55.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-07-05T03:08:28.000Z","updated_at":"2026-05-30T20:17:28.000Z","published_at":"2020-07-19T20:59:48.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Flood_fill\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eFlood fill\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is the algorithm used by bucket-fill tools in many image editors. The idea is that all connected pixels with the input color should have it changed to an output color starting at a pixel P = (x,y). Your task is to implement this algorithm replacing all colors within some tolerance (distance) from the input color with black.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor instance:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eif the input_color=9, tolerance = 0, and P=[1,1], then the following grayscale image,\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[9 9 9 2 9\\n9 9 9 2 9\\n2 2 2 9 9 \\n9 9 9 9 9]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ebecomes,\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[0 0 0 2 9\\n0 0 0 2 9\\n2 2 2 9 9 \\n9 9 9 9 9]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAssume that all pixels have\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Pixel_connectivity#4-connected\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e4-neighborhoods\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. And use the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Taxicab_geometry\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eManhattan distance\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to decide if a color, a 1xn vector, is within tolerance.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"problem_search":{"problems":[{"id":45395,"title":"Create a matrix map of increasing safety levels","description":"The sole nuclear power plant at Grid City suddenly had a meltdown. Luckily, the plant was designed to be in full automation, so no casualties were ever recorded. However, thanks to radiation, the whole city is inhabitable for close to a century.\r\n\r\nYou are then given a portion of the map of Grid City which, for the purpose of this problem, is rendered as an N x N matrix. As the name suggests, Grid City is made of a grid of cells. Your task is to assign a safety level to each cell in this matrix, according to the following rules:\r\n\r\n# Assign _Safety Level 1_ to the cell location of the nuclear power plant, given at row R and column C. This signifies that cell (R,C) is least safe.\r\n# Assign _Safety Level 2_ to the adjacent cells around the power plant, signifying a safer radiation level in this area.\r\n# Continue assigning an increasing number of _Safety Levels_ at succeeding layers of cells surrounding the power plant until you reach the edge of the map.\r\n\r\nWrite a function that accepts three inputs, N, R, and C, and outputs a matrix map of Grid City with _Safety Levels_. Note that you can use any method of populating the matrix as long as it gives the correct answer. You are also ensured that:\r\n\r\n* N, R, C are all integers\r\n* 1 \u003c= N \u003c= 10\r\n* 1 \u003c= R \u003c= N and 1 \u003c= C \u003c= N\r\n\r\nHere are a few examples:\r\n\r\n  \u003e\u003e safety_map(5,5,4) % N = 5, R = 5, C = 4\r\n  ans =\r\n\r\n     5     5     5     5     5\r\n     4     4     4     4     4\r\n     4     3     3     3     3\r\n     4     3     2     2     2\r\n     4     3     2     1     2\r\n\u003e\u003e safety_map(6,2,3) % N = 6, R = 2, C = 3\r\nans =\r\n\r\n     3     2     2     2     3     4\r\n     3     2     1     2     3     4\r\n     3     2     2     2     3     4\r\n     3     3     3     3     3     4\r\n     4     4     4     4     4     4\r\n     5     5     5     5     5     5\r\n","description_html":"\u003cp\u003eThe sole nuclear power plant at Grid City suddenly had a meltdown. Luckily, the plant was designed to be in full automation, so no casualties were ever recorded. However, thanks to radiation, the whole city is inhabitable for close to a century.\u003c/p\u003e\u003cp\u003eYou are then given a portion of the map of Grid City which, for the purpose of this problem, is rendered as an N x N matrix. As the name suggests, Grid City is made of a grid of cells. Your task is to assign a safety level to each cell in this matrix, according to the following rules:\u003c/p\u003e\u003col\u003e\u003cli\u003eAssign \u003ci\u003eSafety Level 1\u003c/i\u003e to the cell location of the nuclear power plant, given at row R and column C. This signifies that cell (R,C) is least safe.\u003c/li\u003e\u003cli\u003eAssign \u003ci\u003eSafety Level 2\u003c/i\u003e to the adjacent cells around the power plant, signifying a safer radiation level in this area.\u003c/li\u003e\u003cli\u003eContinue assigning an increasing number of \u003ci\u003eSafety Levels\u003c/i\u003e at succeeding layers of cells surrounding the power plant until you reach the edge of the map.\u003c/li\u003e\u003c/ol\u003e\u003cp\u003eWrite a function that accepts three inputs, N, R, and C, and outputs a matrix map of Grid City with \u003ci\u003eSafety Levels\u003c/i\u003e. Note that you can use any method of populating the matrix as long as it gives the correct answer. You are also ensured that:\u003c/p\u003e\u003cul\u003e\u003cli\u003eN, R, C are all integers\u003c/li\u003e\u003cli\u003e1 \u0026lt;= N \u0026lt;= 10\u003c/li\u003e\u003cli\u003e1 \u0026lt;= R \u0026lt;= N and 1 \u0026lt;= C \u0026lt;= N\u003c/li\u003e\u003c/ul\u003e\u003cp\u003eHere are a few examples:\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e\u0026gt;\u0026gt; safety_map(5,5,4) % N = 5, R = 5, C = 4\r\nans =\r\n\u003c/pre\u003e\u003cpre\u003e     5     5     5     5     5\r\n     4     4     4     4     4\r\n     4     3     3     3     3\r\n     4     3     2     2     2\r\n     4     3     2     1     2\r\n\u0026gt;\u0026gt; safety_map(6,2,3) % N = 6, R = 2, C = 3\r\nans =\u003c/pre\u003e\u003cpre\u003e     3     2     2     2     3     4\r\n     3     2     1     2     3     4\r\n     3     2     2     2     3     4\r\n     3     3     3     3     3     4\r\n     4     4     4     4     4     4\r\n     5     5     5     5     5     5\u003c/pre\u003e","function_template":"function y = safety_map(N,R,C)\r\n  y = N;\r\nend","test_suite":"%%\r\ny_correct = [3 2 2 2 3 4; 3 2 1 2 3 4; ...\r\n3 2 2 2 3 4; 3 3 3 3 3 4; 4 4 4 4 4 4; ...\r\n5 5 5 5 5 5];\r\nassert(isequal(safety_map(6,2,3),y_correct))\r\n%%\r\ny_correct = [2 2;2 1];\r\nassert(isequal(safety_map(2,2,2),y_correct))\r\n%%\r\ny_correct = [9 8 7 6 5 4 3 2 1 2; ...\r\n9 8 7 6 5 4 3 2 2 2; 9 8 7 6 5 4 3 3 3 3; ...\r\n9 8 7 6 5 4 4 4 4 4; 9 8 7 6 5 5 5 5 5 5; ...\r\n9 8 7 6 6 6 6 6 6 6; 9 8 7 7 7 7 7 7 7 7; ...\r\n9 8 8 8 8 8 8 8 8 8; 9 9 9 9 9 9 9 9 9 9; ...\r\n10 10 10 10 10 10 10 10 10 10];\r\nassert(isequal(safety_map(10,1,9),y_correct))\r\n%%\r\ny_correct = 1;\r\nassert(isequal(safety_map(1,1,1),y_correct))\r\n%%\r\ny_correct = [4 3 2 2; ...\r\n4 3 2 1; 4 3 2 2; 4 3 3 3];\r\nassert(isequal(safety_map(4,2,4),y_correct))\r\n%%\r\ny_correct = [2 1;2 2];\r\nassert(isequal(safety_map(2,1,2),y_correct))\r\n%%\r\ny_correct = [4 4 4 4 4 4 4; 4 3 3 3 3 3 4; ...\r\n4 3 2 2 2 3 4; 4 3 2 1 2 3 4; 4 3 2 2 2 3 4; ...\r\n4 3 3 3 3 3 4; 4 4 4 4 4 4 4];\r\nassert(isequal(safety_map(7,4,4),y_correct))\r\n%%\r\ny_correct = [10 10 10 10 10 10 10 10 10 10; ...\r\n10 9 9 9 9 9 9 9 9 9; 10 9 8 8 8 8 8 8 8 8; ...\r\n10 9 8 7 7 7 7 7 7 7; 10 9 8 7 6 6 6 6 6 6; ...\r\n10 9 8 7 6 5 5 5 5 5; 10 9 8 7 6 5 4 4 4 4; ...\r\n10 9 8 7 6 5 4 3 3 3; 10 9 8 7 6 5 4 3 2 2; ...\r\n10 9 8 7 6 5 4 3 2 1];\r\nassert(isequal(safety_map(10,10,10),y_correct))\r\n%%\r\ny_correct = [3 3 3; 2 2 3; 1 2 3];\r\nassert(isequal(safety_map(3,3,1),y_correct))\r\n%%\r\ny_correct = [2 1 2; 2 2 2; 3 3 3];\r\nassert(isequal(safety_map(3,1,2),y_correct))\r\n","published":true,"deleted":false,"likes_count":7,"comments_count":1,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":92,"test_suite_updated_at":"2020-03-27T15:28:42.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-03-27T15:22:51.000Z","updated_at":"2026-05-03T02:49:25.000Z","published_at":"2020-03-27T15:22:51.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eThe sole nuclear power plant at Grid City suddenly had a meltdown. Luckily, the plant was designed to be in full automation, so no casualties were ever recorded. However, thanks to radiation, the whole city is inhabitable for close to a century.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eYou are then given a portion of the map of Grid City which, for the purpose of this problem, is rendered as an N x N matrix. As the name suggests, Grid City is made of a grid of cells. Your task is to assign a safety level to each cell in this matrix, according to the following rules:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAssign\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSafety Level 1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e to the cell location of the nuclear power plant, given at row R and column C. This signifies that cell (R,C) is least safe.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAssign\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSafety Level 2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e to the adjacent cells around the power plant, signifying a safer radiation level in this area.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"2\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eContinue assigning an increasing number of\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSafety Levels\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e at succeeding layers of cells surrounding the power plant until you reach the edge of the map.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eWrite a function that accepts three inputs, N, R, and C, and outputs a matrix map of Grid City with\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:rPr\u003e\u003cw:i/\u003e\u003c/w:rPr\u003e\u003cw:t\u003eSafety Levels\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Note that you can use any method of populating the matrix as long as it gives the correct answer. You are also ensured that:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eN, R, C are all integers\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1 \u0026lt;= N \u0026lt;= 10\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"ListParagraph\\\"/\u003e\u003cw:numPr\u003e\u003cw:numId w:val=\\\"1\\\"/\u003e\u003c/w:numPr\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e1 \u0026lt;= R \u0026lt;= N and 1 \u0026lt;= C \u0026lt;= N\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eHere are a few examples:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[\u003e\u003e safety_map(5,5,4) % N = 5, R = 5, C = 4\\nans =\\n\\n     5     5     5     5     5\\n     4     4     4     4     4\\n     4     3     3     3     3\\n     4     3     2     2     2\\n     4     3     2     1     2\\n\u003e\u003e safety_map(6,2,3) % N = 6, R = 2, C = 3\\nans =\\n\\n     3     2     2     2     3     4\\n     3     2     1     2     3     4\\n     3     2     2     2     3     4\\n     3     3     3     3     3     4\\n     4     4     4     4     4     4\\n     5     5     5     5     5     5]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"},{"id":46028,"title":"Paint it black!","description":"\u003chttps://en.wikipedia.org/wiki/Flood_fill Flood fill\u003e is the algorithm used by bucket-fill tools in many image editors. The idea is that all connected pixels with the input color should have it changed to an output color starting at a pixel P = (x,y). Your task is to implement this algorithm replacing all colors within some tolerance (distance) from the input color with black. \r\n\r\nFor instance:\r\n\r\nif the input_color=9, tolerance = 0, and P=[1,1], then the following grayscale image,\r\n\r\n  9 9 9 2 9\r\n  9 9 9 2 9\r\n  2 2 2 9 9 \r\n  9 9 9 9 9\r\n\r\nbecomes,\r\n\r\n  0 0 0 2 9\r\n  0 0 0 2 9\r\n  2 2 2 9 9 \r\n  9 9 9 9 9\r\n\r\nAssume that all pixels have \u003chttps://en.wikipedia.org/wiki/Pixel_connectivity#4-connected 4-neighborhoods\u003e. And use the \u003chttps://en.wikipedia.org/wiki/Taxicab_geometry Manhattan distance\u003e to decide if a color, a 1xn vector, is within tolerance. ","description_html":"\u003cp\u003e\u003ca href = \"https://en.wikipedia.org/wiki/Flood_fill\"\u003eFlood fill\u003c/a\u003e is the algorithm used by bucket-fill tools in many image editors. The idea is that all connected pixels with the input color should have it changed to an output color starting at a pixel P = (x,y). Your task is to implement this algorithm replacing all colors within some tolerance (distance) from the input color with black.\u003c/p\u003e\u003cp\u003eFor instance:\u003c/p\u003e\u003cp\u003eif the input_color=9, tolerance = 0, and P=[1,1], then the following grayscale image,\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e9 9 9 2 9\r\n9 9 9 2 9\r\n2 2 2 9 9 \r\n9 9 9 9 9\r\n\u003c/pre\u003e\u003cp\u003ebecomes,\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003e0 0 0 2 9\r\n0 0 0 2 9\r\n2 2 2 9 9 \r\n9 9 9 9 9\r\n\u003c/pre\u003e\u003cp\u003eAssume that all pixels have \u003ca href = \"https://en.wikipedia.org/wiki/Pixel_connectivity#4-connected\"\u003e4-neighborhoods\u003c/a\u003e. And use the \u003ca href = \"https://en.wikipedia.org/wiki/Taxicab_geometry\"\u003eManhattan distance\u003c/a\u003e to decide if a color, a 1xn vector, is within tolerance.\u003c/p\u003e","function_template":"function J = floodfill(I, p, input_color, tolerance)\r\n  J = I;\r\nend","test_suite":"%%\r\nfiletext = fileread('floodfill.m');\r\nassert(isempty(strfind(filetext, 'eval')),       'Eval forbidden.');\r\nassert(isempty(strfind(filetext, 'regexp')),     'Regexp forbidden.');\r\nassert(isempty(strfind(filetext, '!')),          'Shell commands are forbidden.');\r\nassert(isempty(strfind(filetext, 'mlock')),      'mlock is forbidden.');\r\nassert(isempty(strfind(filetext, 'munlock')),    'munlock is forbidden.');\r\n%%\r\np = [1, 1];\r\ninput_color = 9;\r\ntolerance = 0;\r\nI =[9     9     9     2     9;\r\n    9     9     9     2     9;\r\n    2     2     2     9     9;\r\n    9     9     9     9     9];\r\ny_correct = [105\t177\t73\t204\t122\t130\t214\t108\t173\t122\t154\t247\t132\t149\t158\t137\t49\t171\t159\t188\t197\t146\t104\t212\t84\t234\t120\t67\t160\t122\t71\t32\t47\t185\t5\t209\t49\t198\t215\t117\t128\t125\t185\t181\t102\t14\t159\t250\t201\t95\t158\t167\t242\t21\t94\t99\t214\t253\t223\t168\t146\t133\t126\t232];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J)).digest()) == y_correct));\r\n\r\n%%\r\np = [115, 133];\r\ninput_color = 1;\r\ntolerance = 0;\r\nI=imread(fullfile(matlabroot,'toolbox','images','imdata','blobs.png'));\r\ny_correct = [135\t217\t59\t179\t157\t208\t161\t95\t218\t159\t210\t244\t62\t237\t115\t179\t200\t252\t175\t39\t230\t197\t55\t36\t8\t79\t77\t168\t253\t109\t230\t204\t158\t114\t20\t231\t91\t63\t169\t114\t57\t73\t121\t96\t51\t204\t92\t136\t34\t220\t205\t158\t222\t148\t80\t205\t117\t32\t220\t80\t220\t62\t195\t26];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J)).digest()) == y_correct));\r\n\r\n%%\r\np = [170, 70];\r\ninput_color = 1;\r\ntolerance = 0;\r\nI=imread(fullfile(matlabroot,'toolbox','images','imdata','blobs.png'));\r\ny_correct = [45\t21\t215\t148\t24\t198\t184\t203\t33\t38\t222\t107\t60\t205\t211\t33\t76\t33\t62\t139\t133\t58\t42\t238\t77\t128\t243\t214\t143\t201\t181\t109\t218\t80\t3\t41\t44\t130\t179\t21\t51\t87\t168\t161\t118\t110\t241\t214\t236\t45\t210\t130\t28\t94\t170\t39\t9\t240\t10\t103\t158\t207\t214\t203];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J)).digest()) == y_correct));\r\n\r\n%%\r\np = [1500, 1840];\r\ninput_color = 204;\r\ntolerance = 5;\r\nI=imread(fullfile(matlabroot,'toolbox','images','imdata','concordorthophoto.png'));\r\ny_correct = [180\t96\t232\t179\t100\t190\t0\t55\t194\t91\t49\t45\t220\t214\t233\t3\t164\t217\t254\t197\t27\t26\t207\t107\t116\t80\t153\t76\t121\t137\t231\t21\t236\t37\t200\t112\t254\t5\t121\t70\t181\t182\t106\t13\t39\t235\t188\t82\t140\t107\t118\t11\t163\t102\t78\t97\t230\t36\t252\t46\t165\t126\t85\t17];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J)).digest()) == y_correct));\r\n\r\n%%\r\np = [375, 635];\r\ninput_color = [202, 67, 37];\r\ntolerance = 175;\r\nI=imread(fullfile(matlabroot,'toolbox','images','imdata','flamingos.jpg'));\r\ny_correct = [30\t139\t106\t7\t97\t110\t190\t130\t24\t218\t242\t177\t34\t121\t66\t214\t96\t87\t10\t195\t170\t9\t145\t230\t222\t27\t243\t72\t82\t124\t63\t198\t151\t209\t215\t250\t32\t44\t207\t179\t112\t121\t173\t232\t193\t144\t31\t238\t152\t205\t145\t57\t41\t194\t8\t221\t119\t157\t210\t6\t83\t112\t142\t99];\r\nJ = floodfill(I, p, input_color, tolerance);\r\nassert(all(uint8(py.hashlib.sha512(mat2str(J(:))).digest()) == y_correct));\r\n","published":true,"deleted":false,"likes_count":1,"comments_count":6,"created_by":443343,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":3,"test_suite_updated_at":"2020-07-19T21:02:55.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-07-05T03:08:28.000Z","updated_at":"2026-05-30T20:17:28.000Z","published_at":"2020-07-19T20:59:48.000Z","restored_at":null,"restored_by":null,"spam":false,"simulink":false,"admin_reviewed":false,"description_opc":"{\"relationships\":[{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/document\",\"targetMode\":\"\",\"relationshipId\":\"rId1\",\"target\":\"/matlab/document.xml\"},{\"relationshipType\":\"http://schemas.mathworks.com/matlab/code/2013/relationships/output\",\"targetMode\":\"\",\"relationshipId\":\"rId2\",\"target\":\"/matlab/output.xml\"}],\"parts\":[{\"partUri\":\"/matlab/document.xml\",\"relationship\":[],\"contentType\":\"application/vnd.mathworks.matlab.code.document+xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?\u003e\\n\u003cw:document xmlns:w=\\\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\\\"\u003e\u003cw:body\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Flood_fill\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eFlood fill\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e is the algorithm used by bucket-fill tools in many image editors. The idea is that all connected pixels with the input color should have it changed to an output color starting at a pixel P = (x,y). Your task is to implement this algorithm replacing all colors within some tolerance (distance) from the input color with black.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eFor instance:\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eif the input_color=9, tolerance = 0, and P=[1,1], then the following grayscale image,\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[9 9 9 2 9\\n9 9 9 2 9\\n2 2 2 9 9 \\n9 9 9 9 9]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003ebecomes,\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"code\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003e\u003c![CDATA[0 0 0 2 9\\n0 0 0 2 9\\n2 2 2 9 9 \\n9 9 9 9 9]]\u003e\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003cw:p\u003e\u003cw:pPr\u003e\u003cw:pStyle w:val=\\\"text\\\"/\u003e\u003c/w:pPr\u003e\u003cw:r\u003e\u003cw:t\u003eAssume that all pixels have\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Pixel_connectivity#4-connected\\\"\u003e\u003cw:r\u003e\u003cw:t\u003e4-neighborhoods\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e. And use the\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e \u003c/w:t\u003e\u003c/w:r\u003e\u003cw:hyperlink w:docLocation=\\\"https://en.wikipedia.org/wiki/Taxicab_geometry\\\"\u003e\u003cw:r\u003e\u003cw:t\u003eManhattan distance\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:hyperlink\u003e\u003cw:r\u003e\u003cw:t\u003e to decide if a color, a 1xn vector, is within tolerance.\u003c/w:t\u003e\u003c/w:r\u003e\u003c/w:p\u003e\u003c/w:body\u003e\u003c/w:document\u003e\"},{\"partUri\":\"/matlab/output.xml\",\"contentType\":\"text/xml\",\"content\":\"\u003c?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\" standalone=\\\"no\\\" ?\u003e\u003cembeddedOutputs\u003e\u003cmetaData\u003e\u003cevaluationState\u003emanual\u003c/evaluationState\u003e\u003clayoutState\u003ecode\u003c/layoutState\u003e\u003coutputStatus\u003eready\u003c/outputStatus\u003e\u003c/metaData\u003e\u003coutputArray type=\\\"array\\\"/\u003e\u003cregionArray type=\\\"array\\\"/\u003e\u003c/embeddedOutputs\u003e\"}]}"}],"errors":[],"facets":[[{"value":"Chemical Engineering Problems I","count":1,"selected":false}],[{"value":"hard","count":1,"selected":false},{"value":"medium","count":1,"selected":false}]],"term":"tag:\"manhattan distance\"","page":1,"per_page":50,"sort":"map(difficulty_value,0,0,999) asc"}}