{"group":{"id":1,"name":"Community","lockable":false,"created_at":"2012-01-18T18:02:15.000Z","updated_at":"2025-12-14T01:33:56.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":"2025-12-14T00: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":45432,"title":"Pitting corrosion on a metal plate: Find the largest pit","description":"You are given an N x M matrix of _ones_ and _zeros_, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The _ones_ denote areas where the plate has corroded visibly, whereas _zeros_ are areas where the metal is still in good condition. The *corroded areas* (the matrix elements with _ones_) can be found at multiple points on the metal plate. \r\n\r\nA collection of *corroded areas* that are next to each other is called a _pit_. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\r\n\r\nFor this problem, a _pit_ is defined more clearly as follows: Two *corroded areas* at [row _R1_, column _C1_] and [row _R2_, column _C2_] belong _to the same pit_ if and only if |max(abs(R1-R2),abs(C1-C2)) \u003c= 1|. In other words, if two *corroded areas* are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\r\n\r\nThe size of a pit is equal to the number of *corroded areas* (or _ones_) that belong to it.\r\n\r\nWrite a function that accepts a matrix X denoting the image in question. Output the size of the largest pit in this image. You are ensured that the size of the matrix is constrained at 1 \u003c= N \u003c= 20 and 1 \u003c= M \u003c= 20.\r\n\r\nAs an example, consider the following 10 x 10 image (left). The *corroded areas* that belong to the same pit are then labeled, showing that the largest pit is Pit 1, with a size of 6 (right). In contrast, Pits 2, 3, 4 and 5 have size 2, 2, 4, and 4, respectively.\r\n\r\n  Sample test case:\r\n  X = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\r\n       0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\r\n       0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\r\n       0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\r\n       0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\r\n  The largest pit in this image is Pit 1, with size 6.","description_html":"\u003cp\u003eYou are given an N x M matrix of \u003ci\u003eones\u003c/i\u003e and \u003ci\u003ezeros\u003c/i\u003e, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The \u003ci\u003eones\u003c/i\u003e denote areas where the plate has corroded visibly, whereas \u003ci\u003ezeros\u003c/i\u003e are areas where the metal is still in good condition. The \u003cb\u003ecorroded areas\u003c/b\u003e (the matrix elements with \u003ci\u003eones\u003c/i\u003e) can be found at multiple points on the metal plate.\u003c/p\u003e\u003cp\u003eA collection of \u003cb\u003ecorroded areas\u003c/b\u003e that are next to each other is called a \u003ci\u003epit\u003c/i\u003e. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\u003c/p\u003e\u003cp\u003eFor this problem, a \u003ci\u003epit\u003c/i\u003e is defined more clearly as follows: Two \u003cb\u003ecorroded areas\u003c/b\u003e at [row \u003ci\u003eR1\u003c/i\u003e, column \u003ci\u003eC1\u003c/i\u003e] and [row \u003ci\u003eR2\u003c/i\u003e, column \u003ci\u003eC2\u003c/i\u003e] belong \u003ci\u003eto the same pit\u003c/i\u003e if and only if \u003ctt\u003emax(abs(R1-R2),abs(C1-C2)) \u0026lt;= 1\u003c/tt\u003e. In other words, if two \u003cb\u003ecorroded areas\u003c/b\u003e are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\u003c/p\u003e\u003cp\u003eThe size of a pit is equal to the number of \u003cb\u003ecorroded areas\u003c/b\u003e (or \u003ci\u003eones\u003c/i\u003e) that belong to it.\u003c/p\u003e\u003cp\u003eWrite a function that accepts a matrix X denoting the image in question. Output the size of the largest pit in this image. You are ensured that the size of the matrix is constrained at 1 \u0026lt;= N \u0026lt;= 20 and 1 \u0026lt;= M \u0026lt;= 20.\u003c/p\u003e\u003cp\u003eAs an example, consider the following 10 x 10 image (left). The \u003cb\u003ecorroded areas\u003c/b\u003e that belong to the same pit are then labeled, showing that the largest pit is Pit 1, with a size of 6 (right). In contrast, Pits 2, 3, 4 and 5 have size 2, 2, 4, and 4, respectively.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eSample test case:\r\nX = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\r\n     0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\r\n     0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\r\n     0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\r\n     0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n     0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n     0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\r\n     0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\r\n     0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\r\n     0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\r\nThe largest pit in this image is Pit 1, with size 6.\r\n\u003c/pre\u003e","function_template":"function y = largest_pit(X)\r\n  y = X;\r\nend","test_suite":"%%\r\nfiletext = fileread('largest_pit.m')\r\nassert(isempty(strfind(filetext, 'rand')))\r\nassert(isempty(strfind(filetext, 'fileread')))\r\nassert(isempty(strfind(filetext, 'assert')))\r\n%%\r\nx = 1;\r\ny_correct = 1;\r\nassert(isequal(largest_pit(x),y_correct))\r\n%%\r\nx = 0;\r\ny_correct = 0;\r\nassert(isequal(largest_pit(x),y_correct))\r\n%%\r\nx = [1 0 1 0 1];\r\ny_correct = 1;\r\nassert(isequal(largest_pit(x),y_correct))\r\n%%\r\nx = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0\r\n 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 1 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0\r\n 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 1 0\r\n 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0\r\n 1 0 0 1 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0\r\n 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0\r\n 0 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\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":22,"test_suite_updated_at":"2020-04-09T23:45:40.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-04-09T20:15:45.000Z","updated_at":"2025-12-04T14:34:41.000Z","published_at":"2020-04-09T20:58:41.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\u003eYou are given an N x M matrix 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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\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\u003ezeros\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The\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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e denote areas where the plate has corroded visibly, whereas\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\u003ezeros\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are areas where the metal is still in good condition. The\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (the matrix elements 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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e) can be found at multiple points on the metal plate.\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\u003eA collection 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that are next to each other is called a\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\u003epit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\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 this problem, a\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\u003epit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is defined more clearly as follows: Two\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e at [row\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\u003eR1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, column\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\u003eC1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e] and [row\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\u003eR2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, column\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\u003eC2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e] belong\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\u003eto the same pit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e if and only if\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:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emax(abs(R1-R2),abs(C1-C2)) \u0026lt;= 1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. In other words, if two\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\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\u003eThe size of a pit is equal to the 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (or\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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e) that belong to it.\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 a matrix X denoting the image in question. Output the size of the largest pit in this image. You are ensured that the size of the matrix is constrained at 1 \u0026lt;= N \u0026lt;= 20 and 1 \u0026lt;= M \u0026lt;= 20.\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\u003eAs an example, consider the following 10 x 10 image (left). The\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that belong to the same pit are then labeled, showing that the largest pit is Pit 1, with a size of 6 (right). In contrast, Pits 2, 3, 4 and 5 have size 2, 2, 4, and 4, respectively.\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[Sample test case:\\nX = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\\n     0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\\n     0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\\n     0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\\n     0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\\n     0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\\n     0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\\n     0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\\n     0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\\n     0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\\nThe largest pit in this image is Pit 1, with size 6.]]\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":45431,"title":"Pitting corrosion on a metal plate: Count the number of pits","description":"You are given an N x M matrix of _ones_ and _zeros_, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The _ones_ denote areas where the plate has corroded visibly, whereas _zeros_ are areas where the metal is still in good condition. The *corroded areas* (the matrix elements with _ones_) can be found at multiple points on the metal plate. \r\n\r\nA collection of *corroded areas* that are next to each other is called a _pit_. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\r\n\r\nFor this problem, a _pit_ is defined more clearly as follows: Two *corroded areas* at [row _R1_, column _C1_] and [row _R2_, column _C2_] belong to the _same pit_ if and only if |max(abs(R1-R2),abs(C1-C2)) \u003c= 1|. In other words, if two *corroded areas* are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\r\n\r\nWrite a function that accepts a matrix X denoting the image in question. Output the number of distinct separate pits found in this image. You are ensured that the size of the matrix is constrained at 1 \u003c= N \u003c= 20 and 1 \u003c= M \u003c= 20.\r\n\r\nAs an example, consider the following 10 x 10 image (left). The *corroded areas* that belong to the same pit are then labeled, showing that there are 5 distinct separate pits (right).\r\n\r\n Sample test case:\r\n  X = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\r\n       0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\r\n       0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\r\n       0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\r\n       0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\r\n  There are 5 pits in this image.\r\n\r\n\r\n","description_html":"\u003cp\u003eYou are given an N x M matrix of \u003ci\u003eones\u003c/i\u003e and \u003ci\u003ezeros\u003c/i\u003e, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The \u003ci\u003eones\u003c/i\u003e denote areas where the plate has corroded visibly, whereas \u003ci\u003ezeros\u003c/i\u003e are areas where the metal is still in good condition. The \u003cb\u003ecorroded areas\u003c/b\u003e (the matrix elements with \u003ci\u003eones\u003c/i\u003e) can be found at multiple points on the metal plate.\u003c/p\u003e\u003cp\u003eA collection of \u003cb\u003ecorroded areas\u003c/b\u003e that are next to each other is called a \u003ci\u003epit\u003c/i\u003e. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\u003c/p\u003e\u003cp\u003eFor this problem, a \u003ci\u003epit\u003c/i\u003e is defined more clearly as follows: Two \u003cb\u003ecorroded areas\u003c/b\u003e at [row \u003ci\u003eR1\u003c/i\u003e, column \u003ci\u003eC1\u003c/i\u003e] and [row \u003ci\u003eR2\u003c/i\u003e, column \u003ci\u003eC2\u003c/i\u003e] belong to the \u003ci\u003esame pit\u003c/i\u003e if and only if \u003ctt\u003emax(abs(R1-R2),abs(C1-C2)) \u0026lt;= 1\u003c/tt\u003e. In other words, if two \u003cb\u003ecorroded areas\u003c/b\u003e are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\u003c/p\u003e\u003cp\u003eWrite a function that accepts a matrix X denoting the image in question. Output the number of distinct separate pits found in this image. You are ensured that the size of the matrix is constrained at 1 \u0026lt;= N \u0026lt;= 20 and 1 \u0026lt;= M \u0026lt;= 20.\u003c/p\u003e\u003cp\u003eAs an example, consider the following 10 x 10 image (left). The \u003cb\u003ecorroded areas\u003c/b\u003e that belong to the same pit are then labeled, showing that there are 5 distinct separate pits (right).\u003c/p\u003e\u003cpre\u003e Sample test case:\r\n  X = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\r\n       0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\r\n       0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\r\n       0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\r\n       0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\r\n  There are 5 pits in this image.\u003c/pre\u003e","function_template":"function y = count_pits(X)\r\n  y = X;\r\nend","test_suite":"%%\r\nfiletext = fileread('count_pits.m')\r\nassert(isempty(strfind(filetext, 'rand')))\r\nassert(isempty(strfind(filetext, 'fileread')))\r\nassert(isempty(strfind(filetext, 'assert')))\r\n%%\r\nx = 1;\r\ny_correct = 1;\r\nassert(isequal(count_pits(x),y_correct))\r\n%%\r\nx = 0;\r\ny_correct = 0;\r\nassert(isequal(count_pits(x),y_correct))\r\n%%\r\nx = [1 0 1 0 1];\r\ny_correct = 3;\r\nassert(isequal(count_pits(x),y_correct))\r\n%%\r\nx = [0     0     1     0     0     0     0     0     0     0\r\n     0     1     1     0     0     0     0     0     0     0\r\n     0     0     1     1     0     0     0     1     1     0\r\n     0     0     0     1     0     0     0     1     1     0\r\n     0     0     0     0     0     0     0     0     0     0\r\n     0     0     0     0     0     0     0     0     0     0\r\n     0     0     1     0     0     0     0     1     0     0\r\n     0     0     0     1     0     0     0     1     1     0\r\n     0     0     0     0     0     0     0     0     1     0\r\n     0     0     0     0     1     1     0     0     0     0];\r\nassert(isequal(count_pits(x),5))\r\n%%\r\nx = [ 0 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0\r\n0 0 0 0 0 0 0 0 0 1 1 0 1 0 1 0 0 0 0 0\r\n0 1 1 0 0 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1\r\n0 1 1 0 0 1 0 1 0 0 0 0 0 0 1 1 0 0 0 0\r\n0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 1 0 1\r\n0 0 0 1 0 1 0 0 0 0 0 0 1 0 1 0 0 0 0 0\r\n0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0\r\n1 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0 0 0\r\n0 0 0 0 1 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0\r\n0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0\r\n0 0 0 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0\r\n0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 1 0 1\r\n0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0\r\n1 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0\r\n1 0 0 0 0 0 0 0 0 0 0 0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 1 0\r\n 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0\r\n 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n 0 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1\r\n 0 0 0 0 0 0 1 0 0 0 1 0 0 1 0 1 0 0 0 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 0 0 0\r\n 0 0 0 0 0 0 0 1 0 0 0 0 0 1 1 0 0 0 0 0\r\n 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0\r\n 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];\r\nassert(isequal(count_pits(x),30))","published":true,"deleted":false,"likes_count":5,"comments_count":1,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":25,"test_suite_updated_at":"2020-04-09T23:46:12.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-04-09T15:04:42.000Z","updated_at":"2025-11-23T15:32:51.000Z","published_at":"2020-04-09T19:48:23.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\u003eYou are given an N x M matrix 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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\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\u003ezeros\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The\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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e denote areas where the plate has corroded visibly, whereas\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\u003ezeros\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are areas where the metal is still in good condition. The\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (the matrix elements 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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e) can be found at multiple points on the metal plate.\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\u003eA collection 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that are next to each other is called a\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\u003epit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\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 this problem, a\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\u003epit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is defined more clearly as follows: Two\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e at [row\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\u003eR1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, column\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\u003eC1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e] and [row\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\u003eR2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, column\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\u003eC2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e] belong to the\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\u003esame pit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e if and only if\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:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emax(abs(R1-R2),abs(C1-C2)) \u0026lt;= 1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. In other words, if two\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\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 a matrix X denoting the image in question. Output the number of distinct separate pits found in this image. You are ensured that the size of the matrix is constrained at 1 \u0026lt;= N \u0026lt;= 20 and 1 \u0026lt;= M \u0026lt;= 20.\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\u003eAs an example, consider the following 10 x 10 image (left). The\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that belong to the same pit are then labeled, showing that there are 5 distinct separate pits (right).\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[ Sample test case:\\n  X = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\\n       0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\\n       0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\\n       0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\\n       0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\\n       0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\\n       0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\\n       0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\\n  There are 5 pits in this image.]]\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":5,"created_by":443343,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2,"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":"2024-10-30T23:17:50.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":{"errors":[],"problems":[{"id":45432,"title":"Pitting corrosion on a metal plate: Find the largest pit","description":"You are given an N x M matrix of _ones_ and _zeros_, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The _ones_ denote areas where the plate has corroded visibly, whereas _zeros_ are areas where the metal is still in good condition. The *corroded areas* (the matrix elements with _ones_) can be found at multiple points on the metal plate. \r\n\r\nA collection of *corroded areas* that are next to each other is called a _pit_. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\r\n\r\nFor this problem, a _pit_ is defined more clearly as follows: Two *corroded areas* at [row _R1_, column _C1_] and [row _R2_, column _C2_] belong _to the same pit_ if and only if |max(abs(R1-R2),abs(C1-C2)) \u003c= 1|. In other words, if two *corroded areas* are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\r\n\r\nThe size of a pit is equal to the number of *corroded areas* (or _ones_) that belong to it.\r\n\r\nWrite a function that accepts a matrix X denoting the image in question. Output the size of the largest pit in this image. You are ensured that the size of the matrix is constrained at 1 \u003c= N \u003c= 20 and 1 \u003c= M \u003c= 20.\r\n\r\nAs an example, consider the following 10 x 10 image (left). The *corroded areas* that belong to the same pit are then labeled, showing that the largest pit is Pit 1, with a size of 6 (right). In contrast, Pits 2, 3, 4 and 5 have size 2, 2, 4, and 4, respectively.\r\n\r\n  Sample test case:\r\n  X = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\r\n       0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\r\n       0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\r\n       0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\r\n       0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\r\n  The largest pit in this image is Pit 1, with size 6.","description_html":"\u003cp\u003eYou are given an N x M matrix of \u003ci\u003eones\u003c/i\u003e and \u003ci\u003ezeros\u003c/i\u003e, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The \u003ci\u003eones\u003c/i\u003e denote areas where the plate has corroded visibly, whereas \u003ci\u003ezeros\u003c/i\u003e are areas where the metal is still in good condition. The \u003cb\u003ecorroded areas\u003c/b\u003e (the matrix elements with \u003ci\u003eones\u003c/i\u003e) can be found at multiple points on the metal plate.\u003c/p\u003e\u003cp\u003eA collection of \u003cb\u003ecorroded areas\u003c/b\u003e that are next to each other is called a \u003ci\u003epit\u003c/i\u003e. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\u003c/p\u003e\u003cp\u003eFor this problem, a \u003ci\u003epit\u003c/i\u003e is defined more clearly as follows: Two \u003cb\u003ecorroded areas\u003c/b\u003e at [row \u003ci\u003eR1\u003c/i\u003e, column \u003ci\u003eC1\u003c/i\u003e] and [row \u003ci\u003eR2\u003c/i\u003e, column \u003ci\u003eC2\u003c/i\u003e] belong \u003ci\u003eto the same pit\u003c/i\u003e if and only if \u003ctt\u003emax(abs(R1-R2),abs(C1-C2)) \u0026lt;= 1\u003c/tt\u003e. In other words, if two \u003cb\u003ecorroded areas\u003c/b\u003e are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\u003c/p\u003e\u003cp\u003eThe size of a pit is equal to the number of \u003cb\u003ecorroded areas\u003c/b\u003e (or \u003ci\u003eones\u003c/i\u003e) that belong to it.\u003c/p\u003e\u003cp\u003eWrite a function that accepts a matrix X denoting the image in question. Output the size of the largest pit in this image. You are ensured that the size of the matrix is constrained at 1 \u0026lt;= N \u0026lt;= 20 and 1 \u0026lt;= M \u0026lt;= 20.\u003c/p\u003e\u003cp\u003eAs an example, consider the following 10 x 10 image (left). The \u003cb\u003ecorroded areas\u003c/b\u003e that belong to the same pit are then labeled, showing that the largest pit is Pit 1, with a size of 6 (right). In contrast, Pits 2, 3, 4 and 5 have size 2, 2, 4, and 4, respectively.\u003c/p\u003e\u003cpre class=\"language-matlab\"\u003eSample test case:\r\nX = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\r\n     0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\r\n     0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\r\n     0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\r\n     0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n     0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n     0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\r\n     0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\r\n     0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\r\n     0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\r\nThe largest pit in this image is Pit 1, with size 6.\r\n\u003c/pre\u003e","function_template":"function y = largest_pit(X)\r\n  y = X;\r\nend","test_suite":"%%\r\nfiletext = fileread('largest_pit.m')\r\nassert(isempty(strfind(filetext, 'rand')))\r\nassert(isempty(strfind(filetext, 'fileread')))\r\nassert(isempty(strfind(filetext, 'assert')))\r\n%%\r\nx = 1;\r\ny_correct = 1;\r\nassert(isequal(largest_pit(x),y_correct))\r\n%%\r\nx = 0;\r\ny_correct = 0;\r\nassert(isequal(largest_pit(x),y_correct))\r\n%%\r\nx = [1 0 1 0 1];\r\ny_correct = 1;\r\nassert(isequal(largest_pit(x),y_correct))\r\n%%\r\nx = [ 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0\r\n 0 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 1 1 1 1 0 1 1 1 1 0 1 0 0\r\n 0 1 1 0 1 0 1 1 0 1 0 0 1 1 1 1 0\r\n 1 1 1 1 0 1 0 1 1 1 1 0 1 1 0 1 0\r\n 1 1 1 0 0 0 0 0 0 0 0 0 1 0 1 1 1\r\n 1 0 1 0 0 0 0 1 1 0 1 1 1 1 0 1 0\r\n 1 0 0 0 1 0 1 0 1 0 0 0 0 1 1 1 0\r\n 0 1 0 1 0 0 1 1 1 0 1 1 0 0 0 1 0\r\n 1 0 0 0 0 1 0 0 0 0 1 0 0 1 1 0 1\r\n 0 1 1 0 1 0 1 0 0 0 0 1 0 0 0 1 1\r\n 0 0 1 0 0 1 1 0 0 1 1 1 1 1 1 0 1\r\n 0 1 1 0 1 0 0 0 1 0 0 0 0 0 0 0 0\r\n 0 1 1 0 1 1 0 0 1 1 1 0 0 0 0 1 0\r\n 0 0 1 1 0 0 0 0 1 1 1 1 0 1 0 0 0\r\n 1 1 0 1 1 1 0 1 1 0 0 0 0 0 0 0 1\r\n 1 1 0 1 1 0 1 1 1 1 1 1 1 0 1 1 1\r\n 0 1 1 1 0 1 0 1 1 0 0 1 0 0 0 0 1\r\n 1 0 0 1 0 1 0 0 1 1 0 1 0 0 1 0 1\r\n 0 0 0 0 1 1 1 1 0 1 1 0 0 0 0 0 1];\r\nassert(isequal(largest_pit(x),158))\r\n\r\n\r\n","published":true,"deleted":false,"likes_count":3,"comments_count":0,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":22,"test_suite_updated_at":"2020-04-09T23:45:40.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-04-09T20:15:45.000Z","updated_at":"2025-12-04T14:34:41.000Z","published_at":"2020-04-09T20:58:41.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\u003eYou are given an N x M matrix 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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\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\u003ezeros\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The\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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e denote areas where the plate has corroded visibly, whereas\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\u003ezeros\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are areas where the metal is still in good condition. The\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (the matrix elements 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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e) can be found at multiple points on the metal plate.\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\u003eA collection 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that are next to each other is called a\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\u003epit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\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 this problem, a\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\u003epit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is defined more clearly as follows: Two\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e at [row\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\u003eR1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, column\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\u003eC1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e] and [row\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\u003eR2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, column\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\u003eC2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e] belong\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\u003eto the same pit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e if and only if\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:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emax(abs(R1-R2),abs(C1-C2)) \u0026lt;= 1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. In other words, if two\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\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\u003eThe size of a pit is equal to the 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (or\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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e) that belong to it.\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 a matrix X denoting the image in question. Output the size of the largest pit in this image. You are ensured that the size of the matrix is constrained at 1 \u0026lt;= N \u0026lt;= 20 and 1 \u0026lt;= M \u0026lt;= 20.\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\u003eAs an example, consider the following 10 x 10 image (left). The\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that belong to the same pit are then labeled, showing that the largest pit is Pit 1, with a size of 6 (right). In contrast, Pits 2, 3, 4 and 5 have size 2, 2, 4, and 4, respectively.\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[Sample test case:\\nX = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\\n     0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\\n     0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\\n     0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\\n     0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\\n     0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\\n     0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\\n     0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\\n     0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\\n     0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\\nThe largest pit in this image is Pit 1, with size 6.]]\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":45431,"title":"Pitting corrosion on a metal plate: Count the number of pits","description":"You are given an N x M matrix of _ones_ and _zeros_, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The _ones_ denote areas where the plate has corroded visibly, whereas _zeros_ are areas where the metal is still in good condition. The *corroded areas* (the matrix elements with _ones_) can be found at multiple points on the metal plate. \r\n\r\nA collection of *corroded areas* that are next to each other is called a _pit_. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\r\n\r\nFor this problem, a _pit_ is defined more clearly as follows: Two *corroded areas* at [row _R1_, column _C1_] and [row _R2_, column _C2_] belong to the _same pit_ if and only if |max(abs(R1-R2),abs(C1-C2)) \u003c= 1|. In other words, if two *corroded areas* are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\r\n\r\nWrite a function that accepts a matrix X denoting the image in question. Output the number of distinct separate pits found in this image. You are ensured that the size of the matrix is constrained at 1 \u003c= N \u003c= 20 and 1 \u003c= M \u003c= 20.\r\n\r\nAs an example, consider the following 10 x 10 image (left). The *corroded areas* that belong to the same pit are then labeled, showing that there are 5 distinct separate pits (right).\r\n\r\n Sample test case:\r\n  X = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\r\n       0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\r\n       0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\r\n       0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\r\n       0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\r\n  There are 5 pits in this image.\r\n\r\n\r\n","description_html":"\u003cp\u003eYou are given an N x M matrix of \u003ci\u003eones\u003c/i\u003e and \u003ci\u003ezeros\u003c/i\u003e, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The \u003ci\u003eones\u003c/i\u003e denote areas where the plate has corroded visibly, whereas \u003ci\u003ezeros\u003c/i\u003e are areas where the metal is still in good condition. The \u003cb\u003ecorroded areas\u003c/b\u003e (the matrix elements with \u003ci\u003eones\u003c/i\u003e) can be found at multiple points on the metal plate.\u003c/p\u003e\u003cp\u003eA collection of \u003cb\u003ecorroded areas\u003c/b\u003e that are next to each other is called a \u003ci\u003epit\u003c/i\u003e. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\u003c/p\u003e\u003cp\u003eFor this problem, a \u003ci\u003epit\u003c/i\u003e is defined more clearly as follows: Two \u003cb\u003ecorroded areas\u003c/b\u003e at [row \u003ci\u003eR1\u003c/i\u003e, column \u003ci\u003eC1\u003c/i\u003e] and [row \u003ci\u003eR2\u003c/i\u003e, column \u003ci\u003eC2\u003c/i\u003e] belong to the \u003ci\u003esame pit\u003c/i\u003e if and only if \u003ctt\u003emax(abs(R1-R2),abs(C1-C2)) \u0026lt;= 1\u003c/tt\u003e. In other words, if two \u003cb\u003ecorroded areas\u003c/b\u003e are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\u003c/p\u003e\u003cp\u003eWrite a function that accepts a matrix X denoting the image in question. Output the number of distinct separate pits found in this image. You are ensured that the size of the matrix is constrained at 1 \u0026lt;= N \u0026lt;= 20 and 1 \u0026lt;= M \u0026lt;= 20.\u003c/p\u003e\u003cp\u003eAs an example, consider the following 10 x 10 image (left). The \u003cb\u003ecorroded areas\u003c/b\u003e that belong to the same pit are then labeled, showing that there are 5 distinct separate pits (right).\u003c/p\u003e\u003cpre\u003e Sample test case:\r\n  X = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\r\n       0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\r\n       0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\r\n       0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\r\n       0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\r\n       0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\r\n       0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\r\n  There are 5 pits in this image.\u003c/pre\u003e","function_template":"function y = count_pits(X)\r\n  y = X;\r\nend","test_suite":"%%\r\nfiletext = fileread('count_pits.m')\r\nassert(isempty(strfind(filetext, 'rand')))\r\nassert(isempty(strfind(filetext, 'fileread')))\r\nassert(isempty(strfind(filetext, 'assert')))\r\n%%\r\nx = 1;\r\ny_correct = 1;\r\nassert(isequal(count_pits(x),y_correct))\r\n%%\r\nx = 0;\r\ny_correct = 0;\r\nassert(isequal(count_pits(x),y_correct))\r\n%%\r\nx = [1 0 1 0 1];\r\ny_correct = 3;\r\nassert(isequal(count_pits(x),y_correct))\r\n%%\r\nx = [0     0     1     0     0     0     0     0     0     0\r\n     0     1     1     0     0     0     0     0     0     0\r\n     0     0     1     1     0     0     0     1     1     0\r\n     0     0     0     1     0     0     0     1     1     0\r\n     0     0     0     0     0     0     0     0     0     0\r\n     0     0     0     0     0     0     0     0     0     0\r\n     0     0     1     0     0     0     0     1     0     0\r\n     0     0     0     1     0     0     0     1     1     0\r\n     0     0     0     0     0     0     0     0     1     0\r\n     0     0     0     0     1     1     0     0     0     0];\r\nassert(isequal(count_pits(x),5))\r\n%%\r\nx = [ 0 0 0 1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 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 1 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0\r\n 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0\r\n 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 1 0\r\n 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0];\r\nassert(isequal(count_pits(x),30))","published":true,"deleted":false,"likes_count":5,"comments_count":1,"created_by":255320,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":25,"test_suite_updated_at":"2020-04-09T23:46:12.000Z","rescore_all_solutions":false,"group_id":1,"created_at":"2020-04-09T15:04:42.000Z","updated_at":"2025-11-23T15:32:51.000Z","published_at":"2020-04-09T19:48:23.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\u003eYou are given an N x M matrix 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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e and\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\u003ezeros\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, which represents an image of a rectangular metal plate taken from the hull of a ship. Each element of the matrix denotes an area of the plate: The\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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e denote areas where the plate has corroded visibly, whereas\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\u003ezeros\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are areas where the metal is still in good condition. The\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e (the matrix elements 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\u003eones\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e) can be found at multiple points on the metal plate.\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\u003eA collection 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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that are next to each other is called a\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\u003epit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. Pits can come at different sizes, depending on how much they grew over the years. Pitting corrosion is important to quantify because it gives an idea of the reliability of the ship structure.\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 this problem, a\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\u003epit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e is defined more clearly as follows: Two\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e at [row\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\u003eR1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, column\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\u003eC1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e] and [row\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\u003eR2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e, column\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\u003eC2\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e] belong to the\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\u003esame pit\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e if and only if\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:rFonts w:cs=\\\"monospace\\\"/\u003e\u003c/w:rPr\u003e\u003cw:t\u003emax(abs(R1-R2),abs(C1-C2)) \u0026lt;= 1\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e. In other words, if two\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e are adjacent horizontally, vertically, or diagonally, then both are considered to belong to the same pit.\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 a matrix X denoting the image in question. Output the number of distinct separate pits found in this image. You are ensured that the size of the matrix is constrained at 1 \u0026lt;= N \u0026lt;= 20 and 1 \u0026lt;= M \u0026lt;= 20.\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\u003eAs an example, consider the following 10 x 10 image (left). The\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:b/\u003e\u003c/w:rPr\u003e\u003cw:t\u003ecorroded areas\u003c/w:t\u003e\u003c/w:r\u003e\u003cw:r\u003e\u003cw:t\u003e that belong to the same pit are then labeled, showing that there are 5 distinct separate pits (right).\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[ Sample test case:\\n  X = [0 0 1 0 0 0 0 0 0 0        0 0 1 0 0 0 0 0 0 0\\n       0 1 1 0 0 0 0 0 0 0        0 1 1 0 0 0 0 0 0 0\\n       0 0 1 1 0 0 0 1 1 0        0 0 1 1 0 0 0 4 4 0\\n       0 0 0 1 0 0 0 1 1 0        0 0 0 1 0 0 0 4 4 0\\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\\n       0 0 0 0 0 0 0 0 0 0        0 0 0 0 0 0 0 0 0 0\\n       0 0 1 0 0 0 0 1 0 0        0 0 2 0 0 0 0 5 0 0\\n       0 0 0 1 0 0 0 1 1 0        0 0 0 2 0 0 0 5 5 0\\n       0 0 0 0 0 0 0 0 1 0        0 0 0 0 0 0 0 0 5 0\\n       0 0 0 0 1 1 0 0 0 0];      0 0 0 0 3 3 0 0 0 0\\n  There are 5 pits in this image.]]\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":5,"created_by":443343,"edited_by":null,"edited_at":null,"deleted_by":null,"deleted_at":null,"solvers_count":2,"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":"2024-10-30T23:17:50.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\"}]}"}],"term":"tag:\"flood fill\"","current_player_id":null,"fields":[{"name":"page","type":"integer","callback":null,"default":1,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"per_page","type":"integer","callback":null,"default":50,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"sort","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":null,"prepend":true},{"name":"body","type":"text","callback":null,"default":"*:*","directive":null,"facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":false},{"name":"group","type":"string","callback":null,"default":null,"directive":"group","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"difficulty_rating_bin","type":"string","callback":null,"default":null,"directive":"difficulty_rating_bin","facet":true,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"id","type":"integer","callback":null,"default":null,"directive":"id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"tag","type":"string","callback":null,"default":null,"directive":"tag","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"product","type":"string","callback":null,"default":null,"directive":"product","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_at","type":"timeframe","callback":{},"default":null,"directive":"created_at","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"profile_id","type":"integer","callback":null,"default":null,"directive":"author_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"created_by","type":"string","callback":null,"default":null,"directive":"author","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player_id","type":"integer","callback":null,"default":null,"directive":"solver_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"player","type":"string","callback":null,"default":null,"directive":"solver","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"solvers_count","type":"integer","callback":null,"default":null,"directive":"solvers_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"comments_count","type":"integer","callback":null,"default":null,"directive":"comments_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"likes_count","type":"integer","callback":null,"default":null,"directive":"likes_count","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leader_id","type":"integer","callback":null,"default":null,"directive":"leader_id","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true},{"name":"leading_solution","type":"integer","callback":null,"default":null,"directive":"leading_solution","facet":null,"facet_method":"and","operator":null,"param":"term","static":null,"prepend":true}],"filters":[{"name":"asset_type","type":"string","callback":null,"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":null,"static":"\"cody:problem\"","prepend":true},{"name":"profile_id","type":"integer","callback":{},"default":null,"directive":null,"facet":null,"facet_method":"and","operator":null,"param":"author_id","static":null,"prepend":true}],"query":{"params":{"per_page":50,"term":"tag:\"flood fill\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"flood fill\"","","\"","flood fill","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f102a3e1868\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f102a3e17c8\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f102a3e0f08\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f102a3e1ae8\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f102a3e1a48\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f102a3e19a8\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f102a3e1908\u003e":"tag:\"flood fill\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f102a3e1908\u003e":"tag:\"flood fill\""},"queried_facets":{}},"query_backend":{"connection":{"configuration":{"index_url":"http://index-op-v2/solr/","query_url":"http://search-op-v2/solr/","direct_access_index_urls":["http://index-op-v2/solr/"],"direct_access_query_urls":["http://search-op-v2/solr/"],"timeout":10,"vhost":"search","exchange":"search.topic","heartbeat":30,"pre_index_mode":false,"host":"rabbitmq-eks","port":5672,"username":"search","password":"J3bGPZzQ7asjJcCk","virtual_host":"search","indexer":"amqp","http_logging":"true","core":"cody"},"query_connection":{"uri":"http://search-op-v2/solr/cody/","proxy":null,"connection":{"parallel_manager":null,"headers":{"User-Agent":"Faraday v1.0.1"},"params":{},"options":{"params_encoder":"Faraday::FlatParamsEncoder","proxy":null,"bind":null,"timeout":null,"open_timeout":null,"read_timeout":null,"write_timeout":null,"boundary":null,"oauth":null,"context":null,"on_data":null},"ssl":{"verify":true,"ca_file":null,"ca_path":null,"verify_mode":null,"cert_store":null,"client_cert":null,"client_key":null,"certificate":null,"private_key":null,"verify_depth":null,"version":null,"min_version":null,"max_version":null},"default_parallel_manager":null,"builder":{"adapter":{"name":"Faraday::Adapter::NetHttp","args":[],"block":null},"handlers":[{"name":"Faraday::Response::RaiseError","args":[],"block":null}],"app":{"app":{"ssl_cert_store":{"verify_callback":null,"error":null,"error_string":null,"chain":null,"time":null},"app":{},"connection_options":{},"config_block":null}}},"url_prefix":"http://search-op-v2/solr/cody/","manual_proxy":false,"proxy":null},"update_format":"RSolr::JSON::Generator","update_path":"update","options":{"url":"http://search-op-v2/solr/cody"}}},"query":{"params":{"per_page":50,"term":"tag:\"flood fill\"","current_player":null,"sort":"map(difficulty_value,0,0,999) asc"},"parser":"MathWorks::Search::Solr::QueryParser","directives":{"term":{"directives":{"tag":[["tag:\"flood fill\"","","\"","flood fill","\""]]}}},"facets":{"#\u003cMathWorks::Search::Field:0x00007f102a3e1868\u003e":null,"#\u003cMathWorks::Search::Field:0x00007f102a3e17c8\u003e":null},"filters":{"#\u003cMathWorks::Search::Field:0x00007f102a3e0f08\u003e":"\"cody:problem\""},"fields":{"#\u003cMathWorks::Search::Field:0x00007f102a3e1ae8\u003e":1,"#\u003cMathWorks::Search::Field:0x00007f102a3e1a48\u003e":50,"#\u003cMathWorks::Search::Field:0x00007f102a3e19a8\u003e":"map(difficulty_value,0,0,999) asc","#\u003cMathWorks::Search::Field:0x00007f102a3e1908\u003e":"tag:\"flood fill\""},"user_query":{"#\u003cMathWorks::Search::Field:0x00007f102a3e1908\u003e":"tag:\"flood fill\""},"queried_facets":{}},"options":{"fields":["id","difficulty_rating"]},"join":" "},"results":[{"id":45432,"difficulty_rating":"easy-medium"},{"id":45431,"difficulty_rating":"medium"},{"id":46028,"difficulty_rating":"hard"}]}}