{"id":33433,"date":"2023-02-08T06:16:45","date_gmt":"2023-02-08T00:46:45","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/"},"modified":"2023-02-08T06:16:45","modified_gmt":"2023-02-08T00:46:45","slug":"solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/","title":{"rendered":"[Solved] Calculate the number of ways to color an N * M grid using K colors"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-21205829\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"21205829\" data-parentid=\"21205006\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>NOTE: I am assuming that we do not have use every color. If we do, additional checks can easily be applied.<\/p>\n<p>Here is some code that can solve your problem (I think)&#8230; It uses a brute force \/ backtracking algo. Here is the output for 3 colors, and 2 by 3 grid. It prints every combination that it found. For this input, the answer is 54.<\/p>\n<pre><code>Enter number of colors: 3\nEnter number of rows: 2\nEnter number of columns: 3\n121\n212\n\n121\n213\n\n123\n212\n\n121\n232\n\n123\n231\n\n123\n232\n\n131\n212\n\n131\n213\n\n132\n213\n\n121\n312\n\n121\n313\n\n123\n312\n\n131\n312\n\n131\n313\n\n132\n313\n\n131\n323\n\n132\n321\n\n132\n323\n\n212\n121\n\n212\n123\n\n213\n121\n\n212\n131\n\n213\n131\n\n213\n132\n\n231\n123\n\n232\n121\n\n232\n123\n\n212\n321\n\n212\n323\n\n213\n321\n\n231\n312\n\n231\n313\n\n232\n313\n\n231\n323\n\n232\n321\n\n232\n323\n\n312\n121\n\n312\n123\n\n313\n121\n\n312\n131\n\n313\n131\n\n313\n132\n\n321\n132\n\n323\n131\n\n323\n132\n\n312\n231\n\n313\n231\n\n313\n232\n\n321\n212\n\n321\n213\n\n323\n212\n\n321\n232\n\n323\n231\n\n323\n232\n\nThere are 54 different combinations of 3 colors.\n<\/code><\/pre>\n<p>Here is a Java code:<\/p>\n<pre><code>import java.util.*;\n\npublic class ColorChoices\n{\n    int[][] grid;\n    int numberOfColors;\n    int numberOfRows;\n    int numberOfColumns;\n    int numberOfCombinations;\n\n    public static void main(String[] args)\n    {\n        ColorChoices solution = new ColorChoices();\n        solution.begin();\n\n    }\n\n    void begin()\n    {\n        numberOfCombinations = 0;\n        Scanner consoleInput = new Scanner(System.in);\n        System.out.print(\"Enter number of colors: \");\n        numberOfColors = consoleInput.nextInt();\n        System.out.print(\"Enter number of rows: \");\n        numberOfRows = consoleInput.nextInt();\n        System.out.print(\"Enter number of columns: \");\n        numberOfColumns = consoleInput.nextInt();\n        grid = new int[numberOfRows][numberOfColumns];\n\n        solve(0, 0);\n\n        System.out.println(\"There are \" + numberOfCombinations + \" different combinations of \" + numberOfColors + \" colors.\");\n    }\n\n    void solve(int r, int c)\n    {\n        for(int i = 1; i &lt;= numberOfColors; i++)\n        {   \n            if(valid(r, c, i))\n            {\n                grid[r][c] = i;\n                if(r == numberOfRows - 1 &amp;&amp; c == numberOfColumns - 1) \n                {\n                    printBoard();\n                    numberOfCombinations++;\n                }\n                else if(r == numberOfRows - 1) solve(0, c + 1);\n                else solve(r + 1, c);\n            }\n        }\n        grid[r][c] = 0;\n    }\n\n    boolean valid(int r, int c, int n)\n    {\n        return(leftOK(r, c, n) &amp;&amp; rightOK(r, c, n) &amp;&amp;  topOK(r, c, n) &amp;&amp;  bottomOK(r, c, n));\n    }\n\n    boolean leftOK(int r, int c, int n)\n    {\n        if(c == 0) return true;\n        if(grid[r][c - 1] != n) return true;\n        return false;\n    }\n\n    boolean rightOK(int r, int c, int n)\n    {\n        if(c == numberOfColumns - 1) return true;\n        if(grid[r][c + 1] != n) return true;\n        return false;\n    }\n\n    boolean topOK(int r, int c, int n)\n    {\n        if(r == 0) return true;\n        if(grid[r - 1][c] != n) return true;\n        return false;\n    }\n\n    boolean bottomOK(int r, int c, int n)\n    {\n        if(r == numberOfRows - 1) return true;\n        if(grid[r + 1][c] != n) return true;\n        return false;\n    }\n\n    void printBoard()\n    {\n        for(int i = 0; i &lt; numberOfRows; i++)\n        {\n            for(int j = 0; j &lt; numberOfColumns; j++)\n            {\n                System.out.print(grid[i][j]);\n            }\n            System.out.println();\n        }\n        System.out.println();\n    }\n}\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Calculate the number of ways to color an N * M grid using K colors <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] NOTE: I am assuming that we do not have use every color. If we do, additional checks can easily be applied. Here is some code that can solve your problem (I think)&#8230; It uses a brute force \/ backtracking algo. Here is the output for 3 colors, and 2 by 3 grid. It prints &#8230; <a title=\"[Solved] Calculate the number of ways to color an N * M grid using K colors\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/\" aria-label=\"More on [Solved] Calculate the number of ways to color an N * M grid using K colors\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[457,324],"class_list":["post-33433","post","type-post","status-publish","format-standard","hentry","category-solved","tag-algorithm","tag-c"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] Calculate the number of ways to color an N * M grid using K colors - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Calculate the number of ways to color an N * M grid using K colors - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] NOTE: I am assuming that we do not have use every color. If we do, additional checks can easily be applied. Here is some code that can solve your problem (I think)&#8230; It uses a brute force \/ backtracking algo. Here is the output for 3 colors, and 2 by 3 grid. It prints ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-08T00:46:45+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Calculate the number of ways to color an N * M grid using K colors\",\"datePublished\":\"2023-02-08T00:46:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\\\/\"},\"wordCount\":98,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"algorithm\",\"c++\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\\\/\",\"name\":\"[Solved] Calculate the number of ways to color an N * M grid using K colors - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2023-02-08T00:46:45+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Calculate the number of ways to color an N * M grid using K colors\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Calculate the number of ways to color an N * M grid using K colors - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Calculate the number of ways to color an N * M grid using K colors - JassWeb","og_description":"[ad_1] NOTE: I am assuming that we do not have use every color. If we do, additional checks can easily be applied. Here is some code that can solve your problem (I think)&#8230; It uses a brute force \/ backtracking algo. Here is the output for 3 colors, and 2 by 3 grid. It prints ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/","og_site_name":"JassWeb","article_published_time":"2023-02-08T00:46:45+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Calculate the number of ways to color an N * M grid using K colors","datePublished":"2023-02-08T00:46:45+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/"},"wordCount":98,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["algorithm","c++"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/","url":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/","name":"[Solved] Calculate the number of ways to color an N * M grid using K colors - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-08T00:46:45+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-calculate-the-number-of-ways-to-color-an-n-m-grid-using-k-colors\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Calculate the number of ways to color an N * M grid using K colors"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1777008400","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33433","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=33433"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33433\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=33433"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=33433"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=33433"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}