Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find duplicates in a row and column 1

Status
Not open for further replies.

vikasupendra

Programmer
Jun 25, 2011
1
US
Hi guys,
I am writing prolog code to find duplicates of an element in the same row and column. How do I do this?

For ex, if my input grid is like

grid(1,1,1).
grid(2,1,2).
grid(2,1,3).
grid(3,2,1).
grid(4,2,2).
grid(5,2,3).
grid(6,3,1).
grid(7,3,2).
grid(2,3,3).

which represents the grid

1 2 2
3 4 5
6 7 2

I need to find if there duplicates for a number in the same row and column. How do I do this in Prolog?

I was trying something like

finddup(N,X,Y):-
a is X, b is Y,
print('Rule 1 \n'),
finddup(N,X-1,b),
finddup(N,X+1,b),
finddup(N,a,Y-1),
finddup(N,a,Y+1).

finddup(N,_,1):-
print('Rule 2\n'),
grid(N,_,1).

finddup(N,1,_):-
print('Rule 3\n'),
grid(N,1,_).

finddup(N,_,3):-
print('Rule 4\n'),
grid(N,_,3).

finddup(N,3,_):-
print('Rule 5\n')
grid(N,3,_).

Please help me out with the code to check duplicate elements in the same row and colum.
 
You can do that for a line, works with SWI-Prolog
Code:
find_dup_in_line :-
	forall(between(1,3,I),
	       (   setof(V, L^grid(V, I, L), LV),
		   (   length(LV, 3) -> true; format('Dups exist in line ~w~n', [I])))).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top