{"id":3060,"date":"2017-03-08T06:00:26","date_gmt":"2017-03-08T04:00:26","guid":{"rendered":"http:\/\/www.glc.us.es\/~jalonso\/exercitium\/?p=3060"},"modified":"2017-03-08T06:07:51","modified_gmt":"2017-03-08T04:07:51","slug":"numero-de-islas-rectangulares-de-una-matriz-4","status":"publish","type":"post","link":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/numero-de-islas-rectangulares-de-una-matriz-4\/","title":{"rendered":"N\u00famero de islas rectangulares de una matriz"},"content":{"rendered":"<p>En este problema se consideran matrices cuyos elementos son 0 y 1. Los valores 1 aparecen en forma de islas rectangulares separadas por 0 de forma que como m\u00e1ximo las islas son diagonalmente adyacentes. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   ej1, ej2 :: Array (Int,Int) Int\n   ej1 = listArray ((1,1),(6,3))\n                   [0,0,0,\n                    1,1,0,\n                    1,1,0,\n                    0,0,1,\n                    0,0,1,\n                    1,1,0]\n   ej2 = listArray ((1,1),(6,6))\n                   [1,0,0,0,0,0,\n                    1,0,1,1,1,1,\n                    0,0,0,0,0,0,\n                    1,1,1,0,1,1,\n                    1,1,1,0,1,1,\n                    0,0,0,0,1,1]\n<\/pre>\n<p>Definir la funci\u00f3n<\/p>\n<pre lang=\"text\">\n   numeroDeIslas :: Array (Int,Int) Int -> Int\n<\/pre>\n<p>tal que (numeroDeIslas p) es el n\u00famero de islas de la matriz p. Por ejemplo,<\/p>\n<pre lang=\"text\">\n   numeroDeIslas ej1  ==  3\n   numeroDeIslas ej2  ==  4\n<\/pre>\n<h4>Soluciones<\/h4>\n<p>[schedule expon=&#8217;2017-03-15&#8242; expat=\u00bb06:00&#8243;]<\/p>\n<ul>\n<li>Las soluciones se pueden escribir en los comentarios hasta el 15 de marzo.\n<li>El c\u00f3digo se debe escribir entre una l\u00ednea con &#60;pre lang=\u00bbhaskell\u00bb&#62; y otra con &#60;\/pre&#62;\n<\/ul>\n<p>[\/schedule]<\/p>\n<p>[schedule on=&#8217;2017-03-15&#8242; at=\u00bb06:00&#8243;]<\/p>\n<pre lang=\"haskell\">\r\nimport Data.Array\r\n\r\ntype Matriz = Array (Int,Int) Int\r\n\r\nej1, ej2 :: Array (Int,Int) Int\r\nej1 = listArray ((1,1),(6,3))\r\n                [0,0,0,\r\n                 1,1,0,\r\n                 1,1,0,\r\n                 0,0,1,\r\n                 0,0,1,\r\n                 1,1,0]\r\nej2 = listArray ((1,1),(6,6))\r\n                [1,0,0,0,0,0,\r\n                 1,0,1,1,1,1,\r\n                 0,0,0,0,0,0,\r\n                 1,1,1,0,1,1,\r\n                 1,1,1,0,1,1,\r\n                 0,0,0,0,1,1]\r\n\r\nnumeroDeIslas :: Array (Int,Int) Int -> Int\r\nnumeroDeIslas p = \r\n    length [(i,j) | (i,j) <- indices p, \r\n                     verticeSuperiorIzquierdo p (i,j)]\r\n\r\n-- (verticeSuperiorIzquierdo p (i,j)) se verifica si (i,j) es el\r\n-- v\u00e9rtice superior izquierdo de algunas de las islas de la matriz p,\r\n-- Por ejemplo, \r\n--    ghci> [(i,j) | (i,j) <- indices ej1, verticeSuperiorIzquierdo ej1 (i,j)]\r\n--    [(2,1),(4,3),(6,1)]\r\n--    ghci> [(i,j) | (i,j) <- indices ej2, verticeSuperiorIzquierdo ej2 (i,j)]\r\n--    [(1,1),(2,3),(4,1),(4,5)]\r\nverticeSuperiorIzquierdo :: Matriz -> (Int,Int) -> Bool\r\nverticeSuperiorIzquierdo p (i,j) =\r\n    enLadoSuperior p (i,j) && enLadoIzquierdo p (i,j) \r\n\r\n-- (enLadoSuperior p (i,j)) se verifica si (i,j) est\u00e1 en el lado\r\n-- superior de algunas de las islas de la matriz p, Por ejemplo,\r\n--    ghci> [(i,j) | (i,j) <- indices ej1, enLadoSuperior ej1 (i,j)]\r\n--    [(2,1),(2,2),(4,3),(6,1),(6,2)]\r\n--    ghci> [(i,j) | (i,j) <- indices ej2, enLadoSuperior ej2 (i,j)]\r\n--    [(1,1),(2,3),(2,4),(2,5),(2,6),(4,1),(4,2),(4,3),(4,5),(4,6)]\r\nenLadoSuperior :: Matriz -> (Int,Int) -> Bool\r\nenLadoSuperior p (1,j) = p!(1,j) == 1\r\nenLadoSuperior p (i,j) = p!(i,j) == 1 && p!(i-1,j) == 0\r\n\r\n-- (enLadoIzquierdo p (i,j)) se verifica si (i,j) est\u00e1 en el lado\r\n-- izquierdo de algunas de las islas de la matriz p, Por ejemplo,\r\n--    ghci> [(i,j) | (i,j) <- indices ej1, enLadoIzquierdo ej1 (i,j)]\r\n--    [(2,1),(3,1),(4,3),(5,3),(6,1)]\r\n--    ghci> [(i,j) | (i,j) <- indices ej2, enLadoIzquierdo ej2 (i,j)]\r\n--    [(1,1),(2,1),(2,3),(4,1),(4,5),(5,1),(5,5),(6,5)]\r\nenLadoIzquierdo :: Matriz -> (Int,Int) -> Bool\r\nenLadoIzquierdo p (i,1) = p!(i,1) == 1\r\nenLadoIzquierdo p (i,j) = p!(i,j) == 1 && p!(i,j-1) == 0\r\n\r\n-- 2\u00aa soluci\u00f3n\r\n-- ===========\r\n\r\nnumeroDeIslas2 :: Array (Int,Int) Int -> Int\r\nnumeroDeIslas2 p = \r\n    length [(i,j) | (i,j) <- indices p, \r\n                    p!(i,j) == 1,\r\n                    i == 1 || p!(i-1,j) == 0,\r\n                    j == 1 || p!(i,j-1) == 0]  \r\n<\/pre>\n<p>[\/schedule]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>En este problema se consideran matrices cuyos elementos son 0 y 1. Los valores 1 aparecen en forma de islas rectangulares separadas por 0 de forma que como m\u00e1ximo las islas son diagonalmente adyacentes. Por ejemplo, ej1, ej2 :: Array (Int,Int) Int ej1 = listArray ((1,1),(6,3)) [0,0,0, 1,1,0, 1,1,0, 0,0,1, 0,0,1, 1,1,0] ej2 = listArray&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_kad_post_transparent":"","_kad_post_title":"","_kad_post_layout":"","_kad_post_sidebar_id":"","_kad_post_content_style":"","_kad_post_vertical_padding":"","_kad_post_feature":"","_kad_post_feature_position":"","_kad_post_header":false,"_kad_post_footer":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"footnotes":"","_jetpack_memberships_contains_paid_content":false},"categories":[2],"tags":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3060"}],"collection":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/comments?post=3060"}],"version-history":[{"count":1,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3060\/revisions"}],"predecessor-version":[{"id":3061,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/posts\/3060\/revisions\/3061"}],"wp:attachment":[{"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/media?parent=3060"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/categories?post=3060"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.glc.us.es\/~jalonso\/exercitium\/wp-json\/wp\/v2\/tags?post=3060"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}