Are you like the editor, you always like to pick up your mobile phone and open mini games to play in your spare time. This article is about the production process of the C version of the popular game Happy Xiaoxiaole. Friends who are interested can learn about it!
Problem Description
Given a matrix, determine which grid to move to achieve elimination. (define three in a row to eliminate)
It is said to be Huawei's written test question.
Analysis
First write a function to determine whether the grid containing (i, j) can be eliminated.
Then <span style="font-family: 微软雅黑, " microsoft yahei>swap right and down</span>
, and then call the function written above to judge<span style="font-family: 微软雅黑, " microsoft yahei>Whether the two exchanged grids</span>
are eliminated.
The key point is:
- You only need to swap right and downward, because when traversing, subsequent swaps will be repeated. The former one determines whether the right exchange is eliminated, and the latter traversal does not need to determine whether the left exchange is repeated.
- It is necessary to judge whether the two exchanged grids can be eliminated in order to achieve a comprehensive judgment.
Code
// // main.cpp // huawei // // Created by SteveWong on 11/10/2016. // Copyright © 2016 SteveWong. All rights reserved. // #include <iostream> #include <string> #include <vector> #include <ctime> //#include <cstdlib> using namespace std; const int LEN = 8; void pmap(int map[][LEN]) { for (int i = 0; i < LEN; ++i) { for (int j = 0; j < LEN; ++j) { cout << map[i][j] << " "; } cout << endl; } } // 检查以(i,j)为中心的点, 看是否可以消除 bool check(int map[][LEN], int i, int j)// 保证i、j不越界, { if ( (i-1>=0 && i+1<LEN && map[i-1][j]==map[i][j]&&map[i][j]==map[i+1][j]) || (j-1>=0 && j+1<LEN && map[i][j-1]==map[i][j]&&map[i][j]==map[i][j+1]) || (i-2>=0 && map[i-2][j]==map[i-1][j]&&map[i-1][j]==map[i][j]) || (j-2>=0 && map[i][j-2]==map[i][j-1]&&map[i][j-1]==map[i][j]) || (i+2<LEN && map[i+2][j]==map[i+1][j]&&map[i+1][j]==map[i][j]) || (j+2<LEN && map[i][j+2]==map[i][j+1]&&map[i][j+1]==map[i][j]) ) { return true; } return false; } bool swapAndJudge(int m[][LEN], int i, int j)// 保证i、j不越界, 应该对被swap的两个点都做纵向和横向的检查 { int map[LEN][LEN]; for (int ii = 0; ii < LEN; ++ii) { for (int jj = 0; jj < LEN; ++jj) { map[ii][jj] = m[ii][jj]; } } // 原来就可以消除 if (check(map, i, j)) { printf("no need to swap at (%d, %d)\n", i, j); return true; } // 只需要向下换和向右换 // 向下换 if (i + 1 < LEN) { swap(map[i+1][j], map[i][j]); if (check(map, i, j)) { printf("# swap and sweap! (%d, %d)\n", i, j); return true; } if (check(map, i+1, j)) { printf("# swap and sweap! (%d, %d)\n", i+1, j); return true; } swap(map[i+1][j], map[i][j]);// 换回来 } // 向右换 if (j + 1 < LEN) { swap(map[i][j+1], map[i][j]); if (check(map, i, j)) { printf("# swap and sweap! (%d, %d)\n", i, j); return true; } if (check(map, i, j+1)) { printf("# swap and sweap! (%d, %d)\n", i, j+1); return true; } swap(map[i][j+1], map[i][j]);// 换回来 } return false; } void findMinSwap(int map[][LEN]) { for (int i = 0; i < LEN; ++i) { for (int j = 0; j < LEN; ++j) { if (swapAndJudge(map, i, j)) { printf("gotcha! (%d, %d)\n", i, j); } } } } int main(int argc, const char * argv[]) { // insert code here... // std::cout << "Hello, World!\n"; srand(unsigned(time(0))); for (int i = 0; i < LEN; ++i) { for (int j = 0; j < LEN; ++j) { map[i][j] = rand() % 5; } } cout << "xiaoxiaole!\n"; findMinSwap(map); pmap(map); return 0; }
[Recommended course: C Video Tutorial]
The above is the detailed content of 【C++ Fun Program】Happy Xiaoxiaole. For more information, please follow other related articles on the PHP Chinese website!

C# is widely used in enterprise-level applications, game development, mobile applications and web development. 1) In enterprise-level applications, C# is often used for ASP.NETCore to develop WebAPI. 2) In game development, C# is combined with the Unity engine to realize role control and other functions. 3) C# supports polymorphism and asynchronous programming to improve code flexibility and application performance.

C# and .NET are suitable for web, desktop and mobile development. 1) In web development, ASP.NETCore supports cross-platform development. 2) Desktop development uses WPF and WinForms, which are suitable for different needs. 3) Mobile development realizes cross-platform applications through Xamarin.

The C#.NET ecosystem provides rich frameworks and libraries to help developers build applications efficiently. 1.ASP.NETCore is used to build high-performance web applications, 2.EntityFrameworkCore is used for database operations. By understanding the use and best practices of these tools, developers can improve the quality and performance of their applications.

How to deploy a C# .NET app to Azure or AWS? The answer is to use AzureAppService and AWSElasticBeanstalk. 1. On Azure, automate deployment using AzureAppService and AzurePipelines. 2. On AWS, use Amazon ElasticBeanstalk and AWSLambda to implement deployment and serverless compute.

The combination of C# and .NET provides developers with a powerful programming environment. 1) C# supports polymorphism and asynchronous programming, 2) .NET provides cross-platform capabilities and concurrent processing mechanisms, which makes them widely used in desktop, web and mobile application development.

.NETFramework is a software framework, and C# is a programming language. 1..NETFramework provides libraries and services, supporting desktop, web and mobile application development. 2.C# is designed for .NETFramework and supports modern programming functions. 3..NETFramework manages code execution through CLR, and the C# code is compiled into IL and runs by CLR. 4. Use .NETFramework to quickly develop applications, and C# provides advanced functions such as LINQ. 5. Common errors include type conversion and asynchronous programming deadlocks. VisualStudio tools are required for debugging.

C# is a modern, object-oriented programming language developed by Microsoft, and .NET is a development framework provided by Microsoft. C# combines the performance of C and the simplicity of Java, and is suitable for building various applications. The .NET framework supports multiple languages, provides garbage collection mechanisms, and simplifies memory management.

C# and .NET runtime work closely together to empower developers to efficient, powerful and cross-platform development capabilities. 1) C# is a type-safe and object-oriented programming language designed to integrate seamlessly with the .NET framework. 2) The .NET runtime manages the execution of C# code, provides garbage collection, type safety and other services, and ensures efficient and cross-platform operation.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.
