HDU-5031-Lines (DFS), hdu-5031-linesdfs

Source: Internet
Author: User

HDU-5031-Lines (DFS), hdu-5031-linesdfs
Problem DescriptionYou play a game with your friend. he draws several lines on the paper with n × m square grids (see the left figure ). after that, he writes down the number of lines passing through every integer coordinate in a matrix (see the right figure ).


The number of lines passing though coordinate (I, j) is written in cell (I, j) in the right figure. (I, j both start from 0 ).

You are given the matrix written by your friend. You need to figure out the possible minimal number of lines your friend drew on the paper.
InputThe first line of the input contains an integer T indicating the number of test cases (0 <T <= 10 ).

For each test case, the first line contains two integers n, m (1 ≤ n, m ≤ 50) representing the size of the grids on the paper. the following (n + 1) × (m + 1) numbers is what your friend writes. it is guaranteed that the number of lines your friend draws does not exceed 14. each line passes through integer coordinates at least three times.
OutputFor each test case, you need to output the minimal number of lines your friend drew on the paper in a single line.
Sample Input
15 40 1 0 0 10 1 0 1 02 1 1 0 00 3 1 0 01 1 1 0 10 1 0 1 0
 
Sample Output
4
 
Source2014 ACM/ICPC Asia Regional Guangzhou Online
Idea: Enumerate all possible straight lines of the current vertex. For details, see the code.
# Include <cstdio> # include <algorithm> # include <map> using namespace std; int mp [55] [55], ans, n, m, sum; map <double, bool> db; // used to determine the void dfs (int dep, int remain) {if (dep> = ans) return; if (! Remain) {ans = min (ans, dep); return;} int I, j, p, q, cnt; bool flag = 0; for (I = 0; I <n &&! Flag; I ++) {for (j = 0; j <m &&! Flag; j ++) {if (mp [I] [j]) {flag = 1; // locate a vertex, we need to enumerate the feasible straight line if (I = 0) // the vertical line after the change point {for (p = I; p <n; p ++) if (! Mp [p] [j]) break; if (p = n) {for (p = I; p <n; p ++) mp [p] [j] --; dfs (dep + 1, remain-n); for (p = I; p <n; p ++) mp [p] [j] ++ ;}} if (j = 0) // the horizontal line after the change point {for (p = j; p <m; p ++) if (! Mp [I] [p]) break; if (p = m) {for (p = j; p <m; p ++) mp [I] [p] --; dfs (dep + 1, remain-m); for (p = j; p <m; p ++) mp [I] [p] ++;} db. clear (); // clear status for (p = I + 1; p <n; p ++) {for (q = 0; q <m; q ++) {if (mp [p] [q] & p! = I & q! = J) {int x = p-I, y = q-j; if (! Db [double (y)/x]) db [double (y)/x] = 1; // whether the slope straight line has been found else continue; cnt = 1; while (I + cnt * x> = 0 & I + cnt * x <n & j + cnt * y> = 0 & j + cnt * y <m && mp [I + cnt * x] [j + cnt * y]) cnt ++; if (I + cnt * x> = 0 & I + cnt * x <n & j + cnt * y> = 0 & j + cnt * y <m) continue; // if the following clause cannot be consecutive if (I-x> = 0 & I-x <n & j-y> = 0 & j-y <m) continue; // if the previous vertex cannot be consecutive if (cnt <3) continue; // The passing vertex is less than 3 cnt = 1; while (I + cnt * x> = 0 & I + cnt * x <n & j + cnt * y> = 0 & j + cnt * y <m) {mp [I + cnt * x] [j + cnt * y] --; cnt ++;} mp [I] [j] --; dfs (dep + 1, remain-cnt); mp [I] [j] ++; cnt = 1; while (I + cnt * x> = 0 & I + cnt * x <n & j + cnt * y> = 0 & j + cnt * y <m) {mp [I + cnt * x] [j + cnt * y] ++; cnt ++ ;}}}}} int main () {int T, i, j; scanf ("% d", & T); while (T --) {scanf ("% d", & n, & m ); n ++; m ++; sum = 0; for (I = 0; I <n; I ++) {for (j = 0; j <m; j ++) {scanf ("% d", & mp [I] [j]); sum + = mp [I] [j] ;}} ans = min (14, sum/3); dfs (0, sum); printf ("% d \ n", ans );}}



What are the advantages of DFS and DFS?

Through DFS, a sharing point on a server can act as the host of shared resources residing on other servers. DFS transparently links the file server and shared folders and maps them to a single hierarchy so that they can be accessed from one location, while data is actually distributed in different locations. You do not have to go to multiple locations on the network to find the required information. Instead, you only need to connect:
\ DfsServer \ Dfsroot

When a user accesses a folder in the shared folder, the user is redirected to a network location that contains the shared resource. In this way, you only need to know that the DFS root directory is shared to access the shared resources of the entire enterprise.

The DFS topology starts from the root directory of the DFS tree. The DFS root directory at the top of the logical hierarchy maps to a physical share. The DFS link maps the Domain Name System (DNS) name to the UNC name of the destination shared folder or destination DFS root directory. When the DFS client accesses the DFS shared folder, the DFS server maps the DNS name to the UNC name and returns the reference to the client so that it can find the shared folder. Map the DNS name to the UNC name so that the physical location of the data is transparent to the user, so that the user does not need to remember the server that stores the folder. When the DFS client requests a reference from the DFS share, the DFS server uses the Partition Table (PKT) to direct the DFS client to the physical share. For domain-based DFS, PKT is stored in Active Directory; for independent DFS, PKT is stored in the registry. In the network environment, PKT maintains all information about the DFS topology, including its ing to basic physical sharing. After the DFS server directs the DFS client to the copy sharing list corresponding to the requested DFS link, the DFS client connects to a copy of the same site using the Active Directory site topology, if the site does not provide a copy, a copy other than the site is connected.

Distributed Software Systems (Distributed Software Systems) is a Software system that supports Distributed processing and executes tasks in a multi-processor architecture interconnected by a communication network. It includes distributed operating systems, distributed programming languages, compilation (Interpretation) systems, distributed file systems, and distributed database systems.

The distributed operating system manages distributed processing system resources and controls
The types of distributed systems can be roughly classified into three types:

1. distributed data, but there is only one total? No local database.

2. Hierarchical processing. Each layer has its own database.

3. A fully-dispersed distributed network without central control. There are multiple connection methods between nodes, such as loose connection, close connection, and dynamic connection, broadcast and notification connections.

Deep priority (DFS) Algorithm

Depth precedence: O (n + e) indicates that in the graph, if the vertex v is used as the start point, we select a vertex w that has not been searched from the vertex v's adjacent list, and the vertex w continues to be searched by the depth priority method. If no vertex is found, the vertex is stored in the stack. Knowing that no vertex has been traversed by the adjacent vertex u, return to the vertex in the stack, return to the previous vertex to continue to find the vertex that has not been traversed, until all vertices have been searched. Over ~!
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.